我在我的WPF應用程序中使用MEF,並且我不確定是否由於導出它們的方式而導致我的導出沒有進入容器,或者因爲方式我正在查詢容器。試圖在CompositionContainer中找到出口
這裏是我的對象層次:
public interface ICommonInterface
{ }
[InheritedExport]
public abstract class CommonBaseClass<T> : ICommonInterface
{ }
public Class1 : CommonBaseClass<Class1>
{ }
public Class1 : CommonBaseClass<Class2>
{ }
我使用T的實現在基類的問題的範圍之外的原因;這就是爲什麼它在那裏。如果我擺脫對CommonBaseClass仿製藥的,它的繼承類,我可以從我的容器出口爲這樣:
var container = new CompositionContainer(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
var exports = container.GetExportedValues<CommonBaseClass>();
我想要做的,是讓出口的仿製藥版本:
var exports = container.GetExportedValues<CommonBaseClass<ICommonInterface>>();
沒有任何錯誤,只是沒有結果。
卸下其他出口,並增加這1類(使用「typeof運算」,而不是「類型」),我得到一個錯誤鑄造...不能投的下面是'Class1(ContractName =「CommonBaseClass(ICommonInterface)」)'類型的基礎導出值'鍵入'CommonBaseClass'1 [ICommonInterface]' – Random 2013-03-04 16:40:28