Background: I’m attempting to us MEF to import two assemblies implementing IFoo. To do so, unsurprisingly, I’m using [ImportMany] to import all giving me an IEnumerable>. Then my consuming logic determines which of the imported assembly to use based on MetaData “Bar”.
Each exporter is decorated with an ExportMetadata attribute. This works fine when I have:
[Export(typeof(IFoo))]
[ExportMetadata("Bar", "Hello")]
public class Hello : IFoo
{...}
[Export(typeof(IFoo))]
[ExportMetadata("Bar", "World")]
public class World: IFoo
{...}
That is, my IEnumerable has two IFoos.
I want to define one as a default so if there is no need for “Hello” nor “World”, I’ll look for the one with “Default” as my Bar metadata. I have attempted to do this using IsMultiple property like this:
[Export(typeof(IFoo))]
[ExportMetadata("Bar", "Hello", IsMultiple = true)]
[ExportMetadata("Bar", "Default", IsMultiple = true)]
public class Hello : IFoo
{...}
Problem: When I add the IsMultiple property and second attribute, the Hello class is no longer pulled into the resulting IEnumerable.
Question: How do I tag a class for MEF with a second/default designation?
Thanks!
To expose multiple completely separate exports on a class like this, the best approach is to use multiple ‘property exports’ that in this case return ‘this’.