如果没有无参数构造函数,C#/ MEF不适用于基类

我有一个Prim类,它为MEF实现了一个IPrimitiveDecomposer接口并inheritance了Node基类。

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;} } 

但是,当我inheritance一个没有无参数构造函数的类时,MEF的ComposeParts()方法无法导入Prim对象。 我在MSDN中添加了此页面之后的ImportingConstructor属性,因为我收到了没有该属性的编译错误。

 [Export(typeof(IPrimitiveDecomposer))] public class Prim : Node, IPrimitiveDecomposer { [ImportingConstructor] public Prim(int val) : base (val) {} public bool Match(Node node) {return true;} } 

不起作用的代码如下。 当您为Node类提供无参数构造函数时,它可以工作。

 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 PrimitiveDecomposers { get; set; } void mef() { // MEF var catalog = new AggregateCatalog(); catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); var container = new CompositionContainer(catalog); container.ComposeParts(this); } static void Main() { var mef = new Test(); mef.mef(); var res = mef.PrimitiveDecomposers; foreach(var it in res) { Console.WriteLine(it); } } } 

ImportingConstructor属性仅在构造函数的参数是MEF导出对象时才有效。 Prim(int val)构造函数组合失败,因为MEF不知道为构造函数提供什么值。

这种特殊情况看起来更适合MEF工厂模式。

 interface IPrimitiveDecomposerFactory { IPrimitiveDecomposer Create(int value); } [Export(typeof(IPrimitiveDecomposerFactory))] sealed class PrimitiveDecomposerFactor : IPrimitiveDecomposerFactory { public IPrimitiveDecomposer Create(int value) { return new Prim(value); } } 

现在代码可以导入IPrimitiveDecomposerFactory并使用它根据特定的int值创建IPrimitiveDecomposer实例