MEF: Mark interface for export

Go To


Is it possible to mark an interface for export, so that all derived classes will be available for import?

[Export( typeof( IMyInterface ) )]
public interface IMyInterface { ... }

[Import( typeof( IMyInterface ) )]
private readonly ICollection<IMyInterface> m_Concretes = new Collection<IPlugin>();

I don't know which classes are implementing IMyInterface in this example. The classes themselves do not know anything about MEF - and do not use the [Export] attribute.

As long as I do not mark every single class with [Export] it doesn't seem to work for me.

2009-06-16 09:22
by tanascius


In the current preview, you can try putting a [PartExportsInherited] attribute on the interface (along with the Export attribute). I'm not sure whether this will work for interfaces or not, though.

We do plan to add support to putting exports on interfaces.

2009-06-16 13:45
by Daniel Plaisted
Thanks - it works on interfaces, too - tanascius 2009-06-16 14:34


Yes in the current preview on codeplex you can mark the interface with both PartExportsInherited and Export to get have all implementers automatically be exported. In a up comming preview release we will likely be streamlining this to simply place a single attribute, perhaps something like [InheritedExport].

Edit: With MEF preview 6 this can now be done by placing the InheritedExport attribute on the interface.

2009-06-20 02:31
by Wes Haggard
is there a way to do this through attributes?


[InheritedExport] public class Cat : Attribute {


[Cat] public class Ruth {



[ImportMany(Cat)] IEnumerable cats;

//cats will contain "Ruth - Mike 2010-04-21 21:13


Update: Using MEF v4.

public interface IMyInterface

As expected, anything that inherits from IMyInterface will be exported as one.

Use [ImportMany] to have them all injected:

public void MyClass([ImportMany] IEnumerable<IMyInterface> myDerivedObjects)
2014-01-23 09:46
by Lee Oades