MEF 导入不适用于外部程序集
MEF import doesn't work with external assembly
我有以下 MEF 测试代码:
[Import(AllowDefault = true)]
string importedString;
[Import(typeof(IString), AllowDefault = true)]
public IString importedClass;
private void Import(bool fromDll)
{
CompositionContainer MyContainer;
if (fromDll)
{
DirectoryCatalog MyCatalog = new DirectoryCatalog("D:\Source\ClassLibrary\bin\Debug\", "ClassLibrary.dll");
MyContainer = new CompositionContainer(MyCatalog);
}
else
{
AssemblyCatalog MyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
MyContainer = new CompositionContainer(MyCatalog);
}
MyContainer.SatisfyImportsOnce(this);
MessageBox.Show(importedString == null ? "String not found" : importedString, "fromDLL=" + fromDll.ToString());
MessageBox.Show(importedClass == null ? "Class not found" : importedClass.getClassMessage(), "fromDLL=" + fromDll.ToString());
}
导出部分在同一文件中定义如下:
public class MyString
{
[Export()]
public string message = "This string is imported";
}
public interface IString
{
string getClassMessage();
}
[Export(typeof(IString))]
public class MyClass : IString
{
public string getClassMessage()
{
return ("This class is imported");
}
}
现在,如果我调用 Import(false),一切正常,我会收到两个消息框,文本为 "This string is imported" 和 "This class is imported"
但是,如果我创建 ClassLibrary.dll(它的命名空间中只有导出的部分)并调用 Import(true),我会得到 "This string is imported" 消息框,但我得到 "Class not found" 消息。
行为差异的任何原因?我做错了什么吗?
为了完成,我会post回答。
使用 MEF 时,需要注意使用完全相同的类型,这意味着来自相同程序集的相同类型。这就是 MEF 作为插件系统并不是真正有用的原因,因为每次重建包含接口的程序集时,都需要针对它重建每个插件。
当然可以这样做,例如使用 Managed AddIn Framework。有关两者的更多信息,请参阅此 post:Choosing between MEF and MAF (System.AddIn)
我有以下 MEF 测试代码:
[Import(AllowDefault = true)]
string importedString;
[Import(typeof(IString), AllowDefault = true)]
public IString importedClass;
private void Import(bool fromDll)
{
CompositionContainer MyContainer;
if (fromDll)
{
DirectoryCatalog MyCatalog = new DirectoryCatalog("D:\Source\ClassLibrary\bin\Debug\", "ClassLibrary.dll");
MyContainer = new CompositionContainer(MyCatalog);
}
else
{
AssemblyCatalog MyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
MyContainer = new CompositionContainer(MyCatalog);
}
MyContainer.SatisfyImportsOnce(this);
MessageBox.Show(importedString == null ? "String not found" : importedString, "fromDLL=" + fromDll.ToString());
MessageBox.Show(importedClass == null ? "Class not found" : importedClass.getClassMessage(), "fromDLL=" + fromDll.ToString());
}
导出部分在同一文件中定义如下:
public class MyString
{
[Export()]
public string message = "This string is imported";
}
public interface IString
{
string getClassMessage();
}
[Export(typeof(IString))]
public class MyClass : IString
{
public string getClassMessage()
{
return ("This class is imported");
}
}
现在,如果我调用 Import(false),一切正常,我会收到两个消息框,文本为 "This string is imported" 和 "This class is imported"
但是,如果我创建 ClassLibrary.dll(它的命名空间中只有导出的部分)并调用 Import(true),我会得到 "This string is imported" 消息框,但我得到 "Class not found" 消息。 行为差异的任何原因?我做错了什么吗?
为了完成,我会post回答。
使用 MEF 时,需要注意使用完全相同的类型,这意味着来自相同程序集的相同类型。这就是 MEF 作为插件系统并不是真正有用的原因,因为每次重建包含接口的程序集时,都需要针对它重建每个插件。
当然可以这样做,例如使用 Managed AddIn Framework。有关两者的更多信息,请参阅此 post:Choosing between MEF and MAF (System.AddIn)