MEF:标记导出接口
是否可以将接口标记为导出,以便所有派生类都可用于导入?
[Export( typeof( IMyInterface ) )]
public interface IMyInterface { ... }
[Import( typeof( IMyInterface ) )]
private readonly ICollection<IMyInterface> m_Concretes = new Collection<IPlugin>();
我不知道在这个例子中哪些类正在实现IMyInterface
。 这些类本身对 MEF 一无所知,并且不使用 [Export]
属性。
只要我不使用 [Export]
标记每个类,它似乎对我不起作用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在当前预览中,您可以尝试在界面上放置 [PartExportsInherited] 属性(以及 Export 属性)。 不过,我不确定这是否适用于接口。
我们确实计划添加对在接口上进行导出的支持。
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.
是的,在 codeplex 的当前预览中,您可以使用 PartExportsInherited 和 Export 标记接口,以自动导出所有实现者。 在即将发布的预览版本中,我们可能会简化此操作以简单地放置单个属性,也许类似于 [InheritedExport]。
编辑:在 MEF 预览版 6 中,现在可以通过在界面上放置 InheritedExport 属性来完成此操作。
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.
更新:使用 MEF v4。
正如预期的那样,任何从 IMyInterface 继承的内容都将作为一个导出。
使用 [ImportMany] 将它们全部注入:
Update: Using MEF v4.
As expected, anything that inherits from IMyInterface will be exported as one.
Use [ImportMany] to have them all injected: