MEF 导入错误
我有一些简单的导出/导入场景,我无法弄清楚为什么这不起作用。在我的场景中,我有一个 WPF 应用程序和 2 个 ClassLibrary,在 Classlib1 中,我有一个名为 ITestEx1 的接口,如下所示:
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 i 中引用了 Classlib1 并有一个类,如下所示:
using System.ComponentModel.Composition;
using Classlib1;
public class TestMEF
{
[Import(typeof(ITestEx1))]
public ITestEx1 TestE {get; set;}
}
在主 WPF 应用程序中,我引用了 Classlib1 和 ClassLib2,并且在 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
}
请帮助我解决这个问题
i have some simple Export/Import Scenario that i can't figur out why this not work. in my scenario i have one WPF app and 2 ClassLibrary, in Classlib1 i have one interface named ITestEx1 as below :
public interface ITestEx1
{
string Name {get; set;}
}
and 1 derived class Named (TestEx1) as below :
using System.ComponentModel.Composition;
[Export(typeof(ITestEx1))]
public class TestEx1 : ITestEx
{
public Name {get; set;}
}
as you can see this class exported as type of ITestEx1, now in Classlib2 i refrenced Classlib1 and have one class as below :
using System.ComponentModel.Composition;
using Classlib1;
public class TestMEF
{
[Import(typeof(ITestEx1))]
public ITestEx1 TestE {get; set;}
}
and in main WPF application i refrenced both Classlib1 and ClassLib2 and in constructor of MainWindow.xaml i wrote this code for initializing 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);
}
... and in button click i have this :
{
...
var aa = new TestMEF();
aa.TestE.Name = "abc"; // Error, object null refrence
}
Please Help Me to solve this problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您自己创建 TestMEF() ,导入将无法得到满足。您必须从容器请求它:
或者,您可以将以下属性添加到 MainWindow 类,当您调用 _container.ComposeParts(this) 或 _container.SatisfyImportsOnce(this) 时,它将被填充:
If you create TestMEF() yourself the imports won't be satisfied. You have to request it from the container:
Alternatively, you could add the following property to the MainWindow class, and it would get populated when you called _container.ComposeParts(this) or _container.SatisfyImportsOnce(this):
您必须从容器中获取实例,而不是自己创建实例。在这种情况下,您需要一个工厂(在容器中)来动态创建对象。
You have to grab the instance out of the container, not create it yourself. In this case, you would need a factory (in the container) to create the objects on-the-fly.