如何使用MEF的GetExport\ImportDefinition?

发布于 2024-09-26 18:12:45 字数 423 浏览 0 评论 0原文

我有主项目和一系列 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年华零落成诗 2024-10-03 18:12:45

您需要考虑在 MEF 导出的同时提供元数据。

有关详细信息,请参阅以下文章和博客文章:

You need to look into providing metadata along with your MEF exports.

See these articles and blog posts for details:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文