(1) Using the code below, I get exactly 2 elements in my containers of the same exported plugin, and I wonder why:
(2) An additional question that I really canβt implement: how can I extend the framework to handle different types of plugins (for example, have several import types or one import that stores all plugins in dynamic IEnumerable or so on). I want to provide in my static wrapper class one common method that returns the detected plugin as a function of type and metadata matching.
Exported plugin (which is in a separate dll and which indicates when DirectoryCatalog built.
[Export(typeof(IPlugin))] //<---- If this line is commented out then only one item is imported (why?) [PluginAttribute(typeof(StrategyPlugin_Test1), "StrategyPlugin", "Plugin1")] public class StrategyPlugin_Test1 : IPlugin { public void DoSomething() { Console.WriteLine("I do something"); } }
The following code defines strongly typed metadata and imports, as well as a static class that performs MEF functions and must contain detected plugins:
[MetadataAttribute] [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public class PluginAttribute : ExportAttribute { public Type PluginType { get; set; } public string PluginGroupName { get; set; } public string PluginName { get; set; } public PluginAttribute(Type pluginType, string pluginGroupName, string pluginName) : base(typeof(IPlugin)) { PluginType = pluginType; PluginGroupName = pluginGroupName; PluginName = pluginName; } } public interface IPluginAttribute { Type PluginType { get; } string PluginGroupName { get; } string PluginName { get; } } public interface IPlugin { void DoSomething(); } public class PluginDefinition { [ImportMany(typeof(IPlugin))] public IEnumerable<Lazy<IPlugin, IPluginAttribute>> Plugins { get; set; } public PluginDefinition() { } }
Here's a static class that wraps part of the core MEF material:
public static class PluginManager { private static PluginDefinition PluginDefinitions { get; set; } static PluginManager() {} public static void Configure(PluginDefinition pluginDefinitions, IEnumerable<string> pluginDirectories) { AggregateCatalog aggregateCatalog = new AggregateCatalog(new DirectoryCatalog(pluginDirectories.FirstOrDefault())); CompositionContainer container = new CompositionContainer(aggregateCatalog); container.ComposeParts(pluginDefinitions);
source share