I’m using MEF (C# 4.0) for my plugin system. At times it is necessary to get a list of the derived types of all plugins (mainly for XML deserialization). Can this be done without instantiating the plugin?
This works but requires instantiation:
var cat = new DirectoryCatalog(path, "*.dll");
var container = new CompositionContainer(cat);
container.ComposeParts(this);
foreach (var plugin in Plugins)
{
// Would be better if this could be done via Metadata!
DoStuff(plugin.Value.GetType());
}
// ...
[ImportMany]
public Lazy<PluginBase, IPluginMetadata>[] Plugins
{
get;
private set;
}
Q: Is it possible to accomplish this via some ExportAttribute, or some other technique?
Thanks.
MEF cannot provide this information by itself. To understand why, consider the following export via a property:
However, you can still include the type in the export’s metadata (as below or via a custom export attribute that includes the metadata):
and add a
IPluginMetadata.Typemember.