2009-06-16 62 views
4

是否可以標記要導出的界面,以便所有派生類都可以導入?MEF:出口標記界面

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

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

我不知道在這個例子中哪些類正在實現IMyInterface。這些類本身不知道關於MEF的任何信息 - 並且不使用[Export]屬性。

只要我不用[Export]標記每一個班級,它似乎並不適用於我。

回答

4

在當前預覽中,您可以嘗試在接口上添加[PartExportsInherited]屬性(以及Export屬性)。不過,我不確定這是否適用於接口。

我們計劃添加對出口接口的支持。

+0

感謝 - 它的工作原理上的接口了。 – tanascius 2009-06-16 14:34:36

3

是的,在codeplex的當前預覽中,您可以用PartExportsInherited和Export標記接口以獲取所有實現者自動導出。在即將推出的預覽版本中,我們可能會簡化它,只需放置一個屬性,可能類似[InheritedExport]。

編輯:使用MEF預覽6現在可以通過在接口上放置InheritedExport屬性來完成此操作。

+0

有沒有辦法通過屬性來做到這一點? I.E. [InheritedExport] 公共類貓:屬性 { } [貓] 公共類露絲{ } ......... [ImportMany(CAT)] IEnumerable 貓; //貓將包含「露絲」 – Mike 2010-04-21 21:13:53

2

更新:使用MEF v4。

[InheritedExport(typeof(IMyInterface))] 
public interface IMyInterface 
{ 
} 

正如所料,從IMyInterface繼承的任何內容都將導出爲一個。

使用[ImportMany]將它們全部注入:

[ImportingConstructor] 
public void MyClass([ImportMany] IEnumerable<IMyInterface> myDerivedObjects)