C#/MEF不';t使用没有无参数构造函数的基类

C#/MEF不';t使用没有无参数构造函数的基类,c#,constructor,mef,C#,Constructor,Mef,我有一个原语类,它为MEF实现IPrimitiveDecomposer接口并继承节点基类 public class Node { public Node() { } } public interface IPrimitiveDecomposer { bool Match(Node node); } [Export(typeof(IPrimitiveDecomposer))] public class Prim : Node, IPrimitiveDecompo

我有一个原语类,它为MEF实现IPrimitiveDecomposer接口并继承节点基类

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;}
}
但是,当我继承一个没有无参数构造函数的类时,MEF的ComposeParts()方法无法导入Prim对象。我添加了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<IPrimitiveDecomposer> 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);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.Composition;
使用System.ComponentModel.Composition.Hosting;
运用系统反思;
公共类节点
{
公共节点(int-val)
{
}
}
公共接口iPrimitive分解器
{
布尔匹配(Node);
}
[导出(类型(IPrimitiveDecomposer))]
公共类原语:节点,IPrimitiveDecomposer
{
[导入构造函数]
公共Prim(int val):基本值(val)
{}
公共布尔匹配(节点){return true;}
}
公开课考试
{
[ImportMany(typeof(iPrimitiveDecompositioner),AllowRecomposition=true)]
私有IEnumerable基元分解器{get;set;}
void mef()
{
//MEF
var catalog=new AggregateCatalog();
catalog.Catalogs.Add(新的AssemblyCatalog(Assembly.getExecutionGassembly());
var容器=新的合成容器(目录);
容器。组件(本);
}
静态void Main()
{
var mef=新测试();
mef.mef();
var res=mef原始分解器;
foreach(以res表示的var it)
{
控制台写入线(it);
}
}
}

只有当构造函数的参数是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);
  }
}
现在,代码可以导入
iprimitivedecompositerfactory
,并使用它基于特定的
int
值创建
iprimitivedecompositer
实例