我有一些简单的导出/导入场景,我不能找出为什么这不起作用。在我的情况,我有一个WPF应用程序,如低于2 ClassLibrary,在Classlib1我有一个接口命名ITestEx1:如下MEF导入错误
public interface ITestEx1
{
string Name {get; set;}
}
和1派生类的命名(在testEx1):
using System.ComponentModel.Composition;
[Export(typeof(ITestEx1))]
public class TestEx1 : ITestEx
{
public Name {get; set;}
}
你可以看到这个类导出为类型ITestEx1的,现在在Classlib2我refrenced Classlib1和具有如下一类:
using System.ComponentModel.Composition;
using Classlib1;
public class TestMEF
{
[Import(typeof(ITestEx1))]
public ITestEx1 TestE {get; set;}
}
和在主WPF应用程序我refrenced既Classlib1和ClassL IB2和MainWindow.xaml的构造我写了这个代码初始化MEF:
private CompositionContainer _container;
...
public MainWindow()
{
InitializeComponent();
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MainWindow).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(TestEx1).Assemble));
_container = new CompositionContainer(catalog)
_container.ComposeParts(this);
}
...在按钮点击我有这样的:
{
...
var aa = new TestMEF();
aa.TestE.Name = "abc"; // Error, object null refrence
}
请帮我解决这个问题