本文介绍了使用MEF在运行时加载插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序允许用户编写他们可以在运行时实例化的插件(实现IPlugin)。在启动时,将解析插件.dlls的目录,注册所有可用的插件信息。在运行时,提供了一个GUI,允许用户创建任何插件的实例。这工作正常



但现在我看到MEF,希望我能做同样的事情,但是在一个更优雅的方式。



我到目前为止与MEF一起工作:在启动时,我正在导入目录中的所有插件(导出IPlugin)和读出类似名称,类别,作者等的信息...这些信息被编码为插件类的导出元数据属性。导入是懒惰的,所以所有的插件都没有在启动时实例化,这很重要。



问题是,现在我不看到一种在运行时优雅实例化一个选定的插件的方法,因为插件构造函数是导入构造函数的额外的复杂性,它导入对IPluginHost的引用(它需要立即进行一些初始化)。



与plugininfo一起,我在启动期间将相应的导出保存在字典中,所以当GUI要求实例化给定一个特定plugininfo的插件时,我可以访问导出(其中Export.Value是我的实际IPlugin)。但是从那里我怎么可以创建一个插件的实例,并使用IPluginHost组合?



我收集我应该写我自己的ExportProvider服务IPluginHost每当有人问但是,我无法访问程序集或特定插件的类型,可以将其添加到目录中,将目录和ExportProvider添加到容器中,并在该容器上调用.ComposeParts。



我希望我的问题清楚,如果没有,让我尝试简短版本的问题
不是它是MEF的一个标准用例,有一个程序,在启动时懒惰加载插件来解析可用的插件信息,然后在运行时创建给定具体的插件的特定实例?获得所涉及的步骤的代码行是非常好的。

解决方案

如果我理解正确,您正在寻找一种方法来动态创建多个插件实例,可能是同一个插件。 / p>

您需要声明导入类型为,然后根据元数据选择正确的工厂。 ExportFactory.CreateExport 将负责 IPlugin 实例所需的任何导入,如 IPluginHost 你提到。



请注意, ExportFactory 仅在MEF的silverlight版本中早期版本。要在桌面版中获得它,您目前需要(MEF 2 - 预览1)中的最新版本。通过构造函数导入 ExportFactory 也有一个已知问题,因此请使用属性。


My application allows users to write plugins (implementing IPlugin) that they can instantiate at runtime. On startup a directory of plugin .dlls is parsed, registering all the available plugins information. At runtime a GUI is provided that lets users create instances of any of the plugins. This works fine.

But now I see MEF and hope I can do the same, but in a more elegant way codewise.

What I got working so far with MEF: on startup I am doing an import of all plugins in a directory (that export IPlugin) and read out the information like name, category, author, etc... These are encoded as exported metadata attributes to the plugin classes. The import is done lazyly so all the plugins are not instantiated on startup, which is important.

The problem is that now I don't see a way to elegantly instantiate a selected plugin at runtime given the additional complication that the plugins constructor is an importing constructor which is importing a reference to an IPluginHost (which it needs immediately to do some initialization).

Together with a plugininfo I save the respective Export in a dictionary during startup, so when the GUI asks to instantiate a plugin given a specific plugininfo I have access to the Export (where Export.Value is my actual IPlugin). But from there how can I create an instance of the plugin and have it composed with the IPluginHost?

I gather I should write my own ExportProvider that serves the IPluginHost whenever someone asks for it, but I don't have access to the assembly or the type of the specific plugin that would let me add it to a Catalog, add the catalog and ExportProvider to a container and call .ComposeParts on that container.

I hope I made my problem clear, if not, let me try a short version of the question:isn't it a standard usecase for MEF to have a program that lazy-loads plugins on startup to parse the available plugins infos and then at runtime create specific instances given specific plugininfos? would be great to get a codeoutline of the steps involved.

解决方案

If I understand correctly, you are looking for a way to dynamically create multiple plugin instances, potentially of the same plugin.

You need to declare an import of the type ExportFactory<IPlugin,IPluginMetadata> and then select the correct factory based on the metadata. ExportFactory.CreateExport will take care of any imports required by the IPlugin instances, like the IPluginHost you mentioned.

Note that ExportFactory was only in the silverlight edition of MEF in earlier releases. To get it in the desktop edition, you currently need the latest version from codeplex (MEF 2 - Preview 1). There is also a known problem with importing ExportFactory via the constructor, so use a property.

这篇关于使用MEF在运行时加载插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:59