gpt4 book ai didi

c# - MEF 在导入程序集的构造函数中导入程序集不起作用 - 通过调用方法导入是否有效?

转载 作者:太空宇宙 更新时间:2023-11-03 16:16:17 26 4
gpt4 key购买 nike

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

当我在导入的程序集中使用 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”。相反,只需调用“核心”中的 ComposePart。其他一切都由 MEF 处理。于是 child 的代码就变成了这样:

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

关于c# - MEF 在导入程序集的构造函数中导入程序集不起作用 - 通过调用方法导入是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15758704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com