如何使用MEF的GetExport\ImportDefinition?
我有主项目和一系列 dll,它们具有某些接口的特定实现以及视图\视图模型等。我使用 {ImportMany] 来获取所有这些元数据,这允许我选择一个,这给出了我参考了ISystem的具体实现。
我想从选择 ISystem 的任何 dll 中获取 IDisplay 的唯一具体实现,我用 [Export("SomeDisplay", typeof(IDisplay))] 标记了它 到目前为止,我能找到的唯一看起来可能有效的方法是 GetExports(ImportDefinition),但我不明白如何创建一个有效的 ImportDefinition,因为它似乎需要一个特定的合约名称,而这不是直到运行时才知道。
当然,因为我仍然是 MEF n00b,所以我很可能做错了 :D 那么,如果 GetExports 是最好的方法,我该如何让它发挥作用呢?或者我应该使用更好的方法?
谢谢!
I've got main project, and a series of dll's that have specific implementations of some interfaces as well as view\viewmodels, etc. I use {ImportMany] to get them all with some metadata, which allows me to choose one, which gives me a reference to the specific implementation of ISystem.
I'd like to get the only specific implementation of IDisplay from whatever dll the ISystem is chosen from, which I marked with [Export("SomeDisplay", typeof(IDisplay))]
So far, the only thing that I can find that looks like it might work is GetExports(ImportDefinition), but I don't understand how to create an ImportDefinition that would work, as it seems to want a specific contractname, which isn't known until runtime.
Of course since I'm still a MEF n00b, it's a good bet I'm doing it wrong :D
So, if GetExports is the best way, how can I make it work? Or is there a better way I should be using?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要考虑在 MEF 导出的同时提供元数据。
有关详细信息,请参阅以下文章和博客文章:
You need to look into providing metadata along with your MEF exports.
See these articles and blog posts for details: