在已导入程序集的构造函数中导入程序集不起作用——通过调用方法导入程序集是有效的

本文关键字:程序集 导入 有效 调用 方法 不起作用 构造函数 | 更新日期: 2023-09-27 18:06:53

我很好奇为什么会发生以下情况:

当我在导入的程序集中使用ImportMany并在导入的程序集的构造函数中调用compose方法时,我可以看到ImportMany IEnumerable被填充,但是一旦我走出构造函数并回到父程序集的代码中,将鼠标光标移动到刚刚导入的程序集上,它的ImportMany IEnumerable是空的!

如果我在一个从"父"程序集中调用的方法中进行组合,则一切正常。

表示以下内容不能工作:示例代码(父):

    private CompositionContainer modules;
    private AggregateCatalog catalogOfModules = new AggregateCatalog();
    #region MEF Imports
    [Import(typeof(IMyTypedInterface<someOtherInterface>),AllowDefault=true)]
    public someOtherInterface someObject;
    public void StartProgram()
    {
        try
        {
            catalogOfModules.Catalogs.Add(new DirectoryCatalog(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + System.IO.Path.DirectorySeparatorChar + "directoryWichContainsMyAssembly"));
            modules = new CompositionContainer(catalogOfModules);
            modules.ComposeParts(this); 
            someObject.listOfModules.Count; # is 0              
        }
        catch (CompositionException)
        {
        }
    }

示例代码(子):

[Export(typeof(IMyTypedInterface<someOtherInterface>))]
public class ExampleChild{
    [ImportMany(typeof(someInterface))]
    private IEnumerable<someInterface> listOfModules;
    private CompositionContainer modules;
    private AggregateCatalog catalogOfModules = new AggregateCatalog();
    public ExampleChild()
    {
        catalogOfModules.Catalogs.Add(new DirectoryCatalog(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + System.IO.Path.DirectorySeparatorChar + "someDirectoryWithAssemblies"));
        modules = new CompositionContainer(catalogOfModules);
        modules.ComposeParts(this);
        listOfModules.Count; # is 2
    }
}

===========================================

但是这个可以

示例代码(父):

    private CompositionContainer modules;
    private AggregateCatalog catalogOfModules = new AggregateCatalog();
    #region MEF Imports
    [Import(typeof(IMyTypedInterface<someOtherInterface>),AllowDefault=true)]
    public someOtherInterface someObject;
    public void StartProgram()
    {
        try
        {
            catalogOfModules.Catalogs.Add(new DirectoryCatalog(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + System.IO.Path.DirectorySeparatorChar + "directoryWichContainsMyAssembly"));
            modules = new CompositionContainer(catalogOfModules);
            modules.ComposeParts(this);     
            if (someObject != null)
            {
                someObject.ComposeMethod();
                someObject.listOfModules.Count; # is 2
            }
        }
        catch (CompositionException)
        {
        }
    }

示例代码(子):

[Export(typeof(IMyTypedInterface<someOtherInterface>))]
public class ExampleChild : IMyTypedInterface<someOtherInterface>, someOtherInterface{
    [ImportMany(typeof(someInterface))]
    private IEnumerable<someInterface> listOfModules;
    private CompositionContainer modules;
    private AggregateCatalog catalogOfModules = new AggregateCatalog();
    public void ComposeMethod()
    {
        catalogOfModules.Catalogs.Add(new DirectoryCatalog(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + System.IO.Path.DirectorySeparatorChar + "CM"));
        modules = new CompositionContainer(catalogOfModules);
        modules.ComposeParts(this);
        listOfModules.Count; # is 2
    }
}
谁能给我解释一下这种行为的原因?

在已导入程序集的构造函数中导入程序集不起作用——通过调用方法导入程序集是有效的

我的错误是调用了'modules '。每个有子模块的模块中的ComposeParts。相反,只需调用"核心"中的ComposeParts即可。其他的都由MEF处理。所以子节点的代码变成了:

[Export(typeof(IMyTypedInterface<someOtherInterface>))]
public class ExampleChild : IMyTypedInterface<someOtherInterface>, someOtherInterface
{
    [ImportMany(typeof(someInterface))]
    private IEnumerable<someInterface> listOfModules;        
}