C# MEF ComposeParts不创建具有需要注入的参数的类

C# MEF ComposeParts不创建具有需要注入的参数的类,c#,inversion-of-control,unity-container,mef,C#,Inversion Of Control,Unity Container,Mef,我有两个类似的课程 [Export(typeof (IMessageReader))] public class DropboxFolderWatchMessageReader : IMessageReader 还有一个像这样的消息读取器类 public class MessageReader { [ImportMany(typeof(IMessageReader))] private List<IMessageReader> readers; publi

我有两个类似的课程

[Export(typeof (IMessageReader))]
public class DropboxFolderWatchMessageReader : IMessageReader
还有一个像这样的消息读取器类

public class MessageReader
{
    [ImportMany(typeof(IMessageReader))]
    private List<IMessageReader> readers;

    public List<Message> GetMessages()
    {
        var result = new List<Message>();
        readers.ForEach(r => result.AddRange(r.GetMessages()));

        return result;
    }
}
当我尝试运行代码时,我收到一条消息,大意是无法创建我的读者,因为他们不再有默认构造函数。我需要用
[ImportingConstructorAttribute]
来装饰单个构造函数。所以,我补充说

[System.ComponentModel.Composition.ImportingConstructorAttribute]
public DropboxFolderWatchMessageReader(IConfig config)
{
    //...
}
现在它编译并运行正常,但
读取器
集合始终为空

在其他在构造函数中采用相同类型的类中,对象由Unity注入。我的MEF代码中似乎没有出现这种情况

所以,对于这个问题

如何使系统:

  • IConfig
    插入到我的reader类的构造函数中
  • 强制readers集合返回到像以前一样使用导出的reader类填充

我从未将导入构造函数与字段级导入混合使用过,尽管我认为它可以工作。(只要不需要构造函数中的字段或从构造函数调用的方法。)

我的一般建议是不要混合,产生一个构造函数,如:

[System.ComponentModel.Composition.ImportingConstructorAttribute]
public DropboxFolderWatchMessageReader(IConfig config, [ImportMany(typeof(IMessageReader))]
IEnumerable<IMessageReader> readers;)
{
    this.reader = readers.ToList();
    // rest of body
}
[System.ComponentModel.Composition.ImportingConstructorAttribute]
public DropboxFolderWatchMessageReader(IConfig配置,[ImportMany(typeof(IMessageReader))]
IEnumerable readers;)
{
this.reader=readers.ToList();
//身体的其他部分
}
如果需要混合使用这些方法,但需要在对象初始化时使用读取器,则可以实现如下界面:

类DropboxFolderWatchMessageReader:iPartimportsAstifiedNotification
{
[进口数量(类型(IMessageReader))]
私人列表阅读器;
[System.ComponentModel.Composition.ImportingConstructorAttribute]
公共DropboxFolderWatchMessageReader(IConfig配置)
{
//正文为空。此处的读取器仍然为空
}
公共无效OnImportsAssetized()
{
//旧的构造体。
//在这里使用阅读器是安全的,因为所有导入都已满足要求
}
}
[System.ComponentModel.Composition.ImportingConstructorAttribute]
public DropboxFolderWatchMessageReader(IConfig config)
{
    //...
}
[System.ComponentModel.Composition.ImportingConstructorAttribute]
public DropboxFolderWatchMessageReader(IConfig config, [ImportMany(typeof(IMessageReader))]
IEnumerable<IMessageReader> readers;)
{
    this.reader = readers.ToList();
    // rest of body
}
class DropboxFolderWatchMessageReader : IPartImportsSatisfiedNotification
{
    [ImportMany(typeof(IMessageReader))]
    private List<IMessageReader> readers;

    [System.ComponentModel.Composition.ImportingConstructorAttribute]
    public DropboxFolderWatchMessageReader(IConfig config)
    {
        // empty body. readers still null here
    }

    public void OnImportsSatisfied()
    {
        // old constructor body.
        // it's safe to use readers here as all imports have been satisfied
    }
}