Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MEF导入不';不能使用外部组件_C#_.net_Mef - Fatal编程技术网

C# MEF导入不';不能使用外部组件

C# MEF导入不';不能使用外部组件,c#,.net,mef,C#,.net,Mef,我有以下MEF测试代码: [导入(AllowDefault=true)] 字符串导入字符串; [导入(typeof(IString),AllowDefault=true)] 公共IString导入类; 私有void导入(bool-fromDll) { 复合容器; if(fromDll) { DirectoryCatalog MyCatalog=new DirectoryCatalog(“D:\\Source\\ClassLibrary\\bin\\Debug\\”,“ClassLibrary.d

我有以下MEF测试代码:

[导入(AllowDefault=true)]
字符串导入字符串;
[导入(typeof(IString),AllowDefault=true)]
公共IString导入类;
私有void导入(bool-fromDll)
{
复合容器;
if(fromDll)
{
DirectoryCatalog MyCatalog=new DirectoryCatalog(“D:\\Source\\ClassLibrary\\bin\\Debug\\”,“ClassLibrary.dll”);
MyContainer=新合成容器(MyCatalog);
}
其他的
{
AssemblyCatalog MyCatalog=新的AssemblyCatalog(Assembly.getExecutionGassembly());
MyContainer=新合成容器(MyCatalog);
}
MyContainer.SatisfyImportsOnce(本);
Show(importedString==null?“未找到字符串”:importedString,“fromDLL=“+fromDLL.ToString());
MessageBox.Show(importedClass==null?“找不到类”:importedClass.getClassMessage(),“fromDLL=“+fromDLL.ToString());
}
导出部分在同一文件中定义如下:

公共类MyString
{
[导出()]
public string message=“此字符串已导入”;
}
公共接口IString
{
字符串getClassMessage();
}
[导出(类型(IString))]
公共类MyClass:IString
{
公共字符串getClassMessage()
{
退货(“此类已导入”);
}
}
现在,如果我调用Import(false),一切都正常,我确实会收到两个消息框,文本为“thistringisimported”和“thisplasisimported”

但是,如果我创建ClassLibrary.dll(它的名称空间中只有exported部分)并调用Import(true),我确实会得到“thistringisimported”消息框,但会得到“classnotfound”消息。
行为上的差异有什么原因吗?我做错什么了吗?

为了完整起见,我将公布答案

使用MEF时,需要注意使用完全相同的类型,也就是说,来自同一程序集的相同类型。这就是为什么MEF并不是真正有用的插件系统,因为每次你重建包含接口的程序集时,你都需要重建每个插件


当然也有这样做的可能性,例如使用托管加载项框架。有关这两者的更多信息,请参阅本文:

您需要确保使用相同的界面进行导出
string
ist来自mscorlib,但如果app.exe中有IString,ClassLibrary.dll中有IString,它们是MEF的不同接口。那么我如何告诉MEF应用中声明的IString与dll中声明的IString相同?将应用添加为引用,并使用引用的IString接口。(或使用从应用程序和库中引用的单独Interfaces.dll)感谢您的帮助