我很好奇为什么会发生以下情况:
当我在导入的程序集中使用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
}
}
有人可以向我解释这种行为的原因吗?
答案 0 :(得分:0)
我的错误是打电话给&#39; modules.ComposeParts&#39;在每个有子模块的模块中。相反,只需在“核心”中调用ComposeParts即可。其他一切都由MEF处理。所以孩子的代码变成了这个:
[Export(typeof(IMyTypedInterface<someOtherInterface>))]
public class ExampleChild : IMyTypedInterface<someOtherInterface>, someOtherInterface
{
[ImportMany(typeof(someInterface))]
private IEnumerable<someInterface> listOfModules;
}