MEF 导入错误

发布于 2024-10-02 04:59:01 字数 1290 浏览 3 评论 0原文

我有一些简单的导出/导入场景,我无法弄清楚为什么这不起作用。在我的场景中,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所谓啦 2024-10-09 04:59:01

如果您自己创建 TestMEF() ,导入将无法得到满足。您必须从容器请求它:

var aa = _container.GetExport<ITestEx>();

或者,您可以将以下属性添加到 MainWindow 类,当您调用 _container.ComposeParts(this) 或 _container.SatisfyImportsOnce(this) 时,它将被填充:

[Import]
public ITestEx AA { get; set; }

If you create TestMEF() yourself the imports won't be satisfied. You have to request it from the container:

var aa = _container.GetExport<ITestEx>();

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):

[Import]
public ITestEx AA { get; set; }
甜心小果奶 2024-10-09 04:59:01

您必须从容器中获取实例,而不是自己创建实例。在这种情况下,您需要一个工厂(在容器中)来动态创建对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文