tags:

views:

421

answers:

3

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.

+2  A: 

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.

Daniel Plaisted
Thanks - it works on interfaces, too.
tanascius
+1  A: 

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.

Wes Haggard
is there a way to do this through attributes?I.E.[InheritedExport]public class Cat : Attribute{}[Cat]public class Ruth {}.........[ImportMany(Cat)]IEnumerable<Cat> cats;//cats will contain "Ruth"
Mike
A: 

is there a way to do this through attributes?

I.E.

[InheritedExport]
public class Cat : Attribute
{

}


[Cat]
public class Ruth {

}

.........


[ImportMany(Cat)]
IEnumerable<Cat> cats;

//cats will contain "Ruth"
Mike
Is this a question or an answer?
tanascius
my bad - it is a question
Mike