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

时间:2012-01-26 18:13:33

标签: c# constructor mef

我有一个Prim类,它为MEF实现了一个IPrimitiveDecomposer接口并继承了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;}
}

但是,当我继承一个没有无参数构造函数的类时,MEF的ComposeParts()方法无法导入Prim对象。我在this page in 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<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);
        }
    }
}

1 个答案:

答案 0 :(得分:9)

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并根据特定的IPrimitiveDecomposer值使用它创建int实例