简体   繁体   English

如何仅通过MEF加载特定的插件?

[英]How can i load just specific Plugins by MEF?

For my company, I'm developing a simple GUI Framework which can be universally used. 对于我的公司,我正在开发一个可以普遍使用的简单GUI框架。 I want the GUI Framework to read a config file when starting up, which defines, which Plugins should be loaded, where their GUI elements should be arranged, and so on. 我希望GUI框架在启动时读取配置文件,该文件定义了应加载哪些插件,应在何处布置其GUI元素等等。 But the reading of the config file, and also some writing, should happen through a plugin. 但是配置文件的读取以及一些编写应该通过插件进行。 So that's why, this plugin should be loaded first, because before others can be loaded, it needs to read out the config file to identify them. 因此,应该首先加载此插件,因为在加载其他插件之前,需要先读取配置文件以识别它们。

I found out, that I can load just a defined dll like this: var dirCatalog = new DirectoryCatalog(@"..\\..\\Extensions\\","ProgramConfigManager*"); 我发现,我只能像这样加载定义的dll: var dirCatalog = new DirectoryCatalog(@"..\\..\\Extensions\\","ProgramConfigManager*");

But I don't want to rely on the filename. 但是我不想依赖文件名。 My intention is to first load the MEF plugin with the Interface ISAProgramConfigManagerContent : 我的意图是首先使用ISAProgramConfigManagerContent接口加载MEF插件:

[Import(typeof(ISAProgramConfigManagerContent))]
SAProgramConfigManagerContent PCM;

After this, the other Plugins, but just the ones in the config file should be loaded into this: 之后,其他插件(但仅配置文件中的插件)应加载到此插件中:

[ImportMany(typeof(IPlugin))]
List<IPlugin> Plugins;

Is there a way, to first just load the special Plugin which handles the config file, by filtering the contracts and just compose the one of type ISAProgramConfigManagerContent ? 有没有一种方法,首先通过过滤合同并仅ISAProgramConfigManagerContent类型的插件来加载处理配置文件的特殊插件?

Thanks in advance. 提前致谢。

When importing from other assemblies, you must tell MEF which in files to have a look. 从其他程序集导入时,必须告诉MEF哪些文件需要查看。 This is either a directory, by file globbing, or any other custom means. 通过文件通配,这可以是目录,也可以是任何其他自定义方式。 Otherwise MEF has no way where to look for the export. 否则,MEF无法在哪里寻找出口。

Having said this, you could first setup a catalog to load the plugin manager. 话虽如此,您可以先设置一个目录来加载插件管理器。 And then, based on that, setup another catalog with the entries that you want to load and import the plugins programmatically from that. 然后,基于此设置另一个目录,其中包含您要加载的条目,并以编程方式从中导入插件。 You can also use an AggregateCatalog to unite several DirectoryCatalog s. 您还可以使用AggregateCatalog组合多个DirectoryCatalog

Something like: 就像是:

var container = new CompositionContainer(aggregateCatalog);
IEnumerable<IPlugin> plugins = container.GetExports<IPlugin>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM