如何使用 MEF 导入多个插件/部件?

发布于 2024-08-05 14:36:08 字数 1231 浏览 2 评论 0原文

我是 MEF 新手,正在尝试使用它来构建插件系统,但卡在第一步。

我正在关注 Andrew Whitechapel 撰写的文章。我已经下载了他的示例代码,它运行正常(如果您删除其中一个“导出”程序集 - 它们在他的示例中是互斥的 - 并引用 MEF 程序集)。

该示例演示了导入单个零件。我想导入多个部分(全部基于相同的界面)。因此,我将示例代码更改如下:

[Import]
// OLD - public Interface.ICalculate Calculate { get; set; }
public IEnumerable<Interface.ICalculate> Calculators { get; set; }

// OLD - Console.WriteLine(
// OLD -     String.Format("{0}", Calculate.Circumference(4)));
foreach (Interface.ICalculate calculator in Calculators)
{
    Console.WriteLine(
    String.Format("{0}", calculator.Circumference(4)));
}

我还为 IEnumerable 导入了 System.Collections.Generic。

关键的变化是第一个。据我了解,这将允许我从多个组件导入零件。但是,我收到以下错误:

No valid exports were found that match the constraint

此时我什至还没有添加多个“插件”程序集。还是只有那一个吧

为了完整起见,这是他在“插件”类库中的导出定义(我没有触及):

[Export(typeof(Interface.ICalculate))]
public class Calculate : Interface.ICalculate

有什么想法吗?我在这里摸不着头脑。我搜索过 SO 和 MEF 论坛,但可以找到任何可以启发我的东西。

我正在使用 VS 2008 SP1(未安装 2010 beta)和最新的 System.ComponentModel.Composition 程序集(2009.26.8.0)。

I'm new to MEF and am trying to use it to build a plug-in system, but am stuck at step one.

I'm following an article by Andrew Whitechapel. I've downloaded his sample code an it runs OK (if you remove one of the "exporting" assemblies - they are mutually exclusive in his sample - and reference the MEF assembly).

The sample illustrates importing a single part. I want to import multiple parts (all based on the same interface). So, I change the sample code as follows:

[Import]
// OLD - public Interface.ICalculate Calculate { get; set; }
public IEnumerable<Interface.ICalculate> Calculators { get; set; }

// OLD - Console.WriteLine(
// OLD -     String.Format("{0}", Calculate.Circumference(4)));
foreach (Interface.ICalculate calculator in Calculators)
{
    Console.WriteLine(
    String.Format("{0}", calculator.Circumference(4)));
}

I also imported System.Collections.Generic for the IEnumerable.

The key change is the first one. As I understand it, this will allow me to import parts from multiple assemblies. However, I get the following error:

No valid exports were found that match the constraint

At this point I haven't even added multiple "plugin" assemblies. Still just have the one.

For completeness here's his export definition (which I haven't touched) in the "plugin" class library:

[Export(typeof(Interface.ICalculate))]
public class Calculate : Interface.ICalculate

Any ideas? I'm scratching my head here. I've searched SO and the MEF forums, but could find anything to enlighten me.

I'm using VS 2008 SP1 (no 2010 beta installed) and the latest System.ComponentModel.Composition assembly (2009.26.8.0).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

笑饮青盏花 2024-08-12 14:36:08

MEF 预览版 5 改变了这一点。您现在需要使用 ImportManyAttribute 而不是 ImportAttribute:

[ImportMany]
public IEnumerable<Intertface.ICalculate> Calculators { get; set; }

有关详细信息,请参阅 PR5 公告

MEF Preview Release 5 changed this. You now need to use ImportManyAttribute instead of ImportAttribute:

[ImportMany]
public IEnumerable<Intertface.ICalculate> Calculators { get; set; }

For details, see the announcement for PR5.

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