没有无参数构造函数,C#/MEF不能与基类一起使用 [英] C#/MEF doesn't work with a base class without parameterless constructor

查看:91
本文介绍了没有无参数构造函数,C#/MEF不能与基类一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Prim类,该类为MEF实现IPrimitiveDecomposer接口,并继承Node基类.

I have a Prim class that implements an IPrimitiveDecomposer interface for MEF and inherits 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;}
}

但是,当我继承一个没有无参数构造函数的类时,MEF的ComposeParts()方法无法导入Prim对象.我在此MSDN中的页面中添加了ImportingConstructor的属性,没有属性的编译错误.

However, when I inherit a class that doesn't have a parameterless constructor, MEF's ComposeParts() method cannot import the Prim object. I added the attribute of ImportingConstructor following this page in MSDN, as I got compilation error without the attribute.

[Export(typeof(IPrimitiveDecomposer))]
public class Prim : Node, IPrimitiveDecomposer
{
    [ImportingConstructor] 
    public Prim(int val) : base (val)
    {}

    public bool Match(Node node) {return true;}
}

无效的代码如下.当您为Node类提供无参数的构造函数时,它将起作用.

The code that doesn't work is as follows. When you provide the 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()
    {
        // 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不知道要为构造函数提供什么值.

The ImportingConstructor attribute only works when the parameters to the constructor are MEF exported objects. The Prim(int val) constructors fails composition because MEF doesn't know what value to provide for the constructor.

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

This particular scenario looks like it's much more suited for a MEF factory pattern.

interface IPrimitiveDecomposerFactory {
  IPrimitiveDecomposer Create(int value);
}

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

现在,代码可以导入IPrimitiveDecomposerFactory并使用它基于特定的int值创建实例

Now code can import the IPrimitiveDecomposerFactory and use it to create IPrimitiveDecomposer instance based on specific int values

这篇关于没有无参数构造函数,C#/MEF不能与基类一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆