简体   繁体   中英

How can i load just specific Plugins by MEF?

For my company, I'm developing a simple GUI Framework which can be universally used. 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. 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*");

But I don't want to rely on the filename. My intention is to first load the MEF plugin with the Interface ISAProgramConfigManagerContent :

[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 ?

Thanks in advance.

When importing from other assemblies, you must tell MEF which in files to have a look. This is either a directory, by file globbing, or any other custom means. Otherwise MEF has no way where to look for the export.

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.

Something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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