使用 MEF 加载多个插件实例

发布于 2024-09-04 04:07:14 字数 1276 浏览 8 评论 0原文

在我的上一个应用程序中,使用 MEF 加载插件运行得很好,但现在我遇到了一个新问题。我有一个解决方案,我在这个问题的末尾解释,但我正在寻找其他方法来做到这一点。

假设我有一个名为 ApplianceInterface 的接口。我还有两个继承自 ApplianceInterface 的插件,我们称它们为 Blender 和 Processor。现在,我想在我的应用程序中拥有多个搅拌机和处理器,但我不确定如何正确实例化它们。

之前,我只需使用 ImportMany 属性,并在调用 ComposeParts 时,我的应用程序将加载 Blender 和 Processor。例如:

[ImportMany(typeof(ApplianceInterface))]
private IEnumerable<ApplianceInterface> Appliances { get; set; }

我的搅拌机和处理器插件将被归为这样:

[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(typeof(MyInterface)]
public class Blender : ApplianceInterface
{
    ...
}

但这最终对我来说是用一台搅拌机和一台处理器填充设备。我需要能够创建任意数量的 Blender 和 Processor 对象。

现在,从文档中我了解到 [PartCreationPolicy(CreationPolicy.NonShared)] 允许 MEF 每次创建一个新实例,但是是否有类似的“神奇”方式来创建特定数量的实例使用 MEF 的实例?到目前为止,我一直依靠 [Import][ImportMany] 来解析程序集。

我唯一的选择是使用全局容器,然后使用 GetExportedValue<> 手动解析导出吗?我尝试过 GetExportedValue<>这个实现对我来说确实工作得很好,但我只是好奇是否有更好、更容易接受的方法来做到这一点。

更新

我刚刚意识到一个大错误,并且 GetExportedValue<>这不是我真正想要的。我正在迭代 IEnumerable,当我获得匹配项(基于某些参数)时,我想实例化当前值的新对象。获取导出值<>如果我有两个不同的插件都导出 ApplianceInterface,则最终会失败。

我认为我的问题现在不同了,可能是 C# 特定的问题。

In my last application, using MEF to load plugins went just fine, but now I'm running into a new issue. I have a solution for it that I explain at the end of this question, but I'm looking for other ways to do it.

Let's say I have an interface called ApplianceInterface. I also have two plugins that inherit from ApplianceInterface, let's call them Blender and Processor. Now, I would like to have multiple Blenders and Processors in my application, but I am not sure how to instantiate them properly.

Before, I would simply use the ImportMany attribute and upon calling ComposeParts, my application would load Blender and Processor. For example:

[ImportMany(typeof(ApplianceInterface))]
private IEnumerable<ApplianceInterface> Appliances { get; set; }

and my Blender and Processor plugins would be attributed like this:

[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(typeof(MyInterface)]
public class Blender : ApplianceInterface
{
    ...
}

but what this ends up doing for me is populating Appliances with one Blender and one Processor. I need to be able to create an arbitrary number of Blender and Processor objects.

Now, from the documentation I understand that [PartCreationPolicy(CreationPolicy.NonShared)] is what allows MEF to create a new instance each time, but is there a similar "magical" way to create a specific number of instances of something using MEF? Up until now, I've relied on [Import] and [ImportMany] to resolve the assemblies.

Is my only option to use a global container, and then resolve the export manually using GetExportedValue<>? I have tried GetExportedValue<> and that implementation does work fine for me, but I was just curious if there is a better, more accepted way to do it.

UPDATE

I just realized a big mistake, and GetExportedValue<> isn't what I really want. I'm iterating over an IEnumerable, and when I get a match (based on some parameters), I want to instantiate a new object of the current value. GetExportedValue<> ends up failing in the case where I have two different plugins that both export ApplianceInterface.

I think my question is different now, and is probably a C# specific one.

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

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

发布评论

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

评论(1

她说她爱他 2024-09-11 04:07:14

ExportFactory 正是您正在寻找的,但它目前仅在 Silverlight 中可用。这是一个解释如何在桌面上处理此问题的问题: Multiple Instances of a单个 MEF DLL

ExportFactory is what you are looking for, but it is currently only available in Silverlight. Here is a question that explains how to handle this on the desktop: Multiple Instances of a single MEF DLL

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