I have a Prim class that implements the IPrimitiveDecomposer interface for MEF and inherits the Node base class.
public class Node { public Node() { } } public interface IPrimitiveDecomposer { bool Match(Node node); } [Export(typeof(IPrimitiveDecomposer))] public class Prim : Node, IPrimitiveDecomposer { public bool Match(Node node) {return true;} }
However, when I inherit a class that does not have a constructor without parameters, the MEF ComposeParts () method cannot import the Prim object. I added the ImportingConstructor attribute after this page on MSDN , since I received a compilation error without an attribute.
[Export(typeof(IPrimitiveDecomposer))] public class Prim : Node, IPrimitiveDecomposer { [ImportingConstructor] public Prim(int val) : base (val) {} public bool Match(Node node) {return true;} }
Code that does not work is as follows. When you provide a parameterless constructor for the Node class, it works.
using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.Reflection; public class Node { public Node(int val) { } } public interface IPrimitiveDecomposer { bool Match(Node node); } [Export(typeof(IPrimitiveDecomposer))] public class Prim : Node, IPrimitiveDecomposer { [ImportingConstructor] public Prim(int val) : base (val) {} public bool Match(Node node) {return true;} } public class Test { [ImportMany(typeof(IPrimitiveDecomposer), AllowRecomposition = true)] private IEnumerable<IPrimitiveDecomposer> PrimitiveDecomposers { get; set; } void mef() {
source share