简体   繁体   English

MEF:如果我有多个出口但只需要一次导入怎么办?

[英]MEF: What if I have multiple exports but need only one import?

I'm trying to wrap my mind around MEF. 我想把我的思绪包裹在MEF周围。 There is one thing I don't understand. 有一点我不明白。

Assume that I have an interface, named ISomething, which is a contract, and I have more than one assemblies in a folder that contains my application, and I have no idea how many ISomething implementations are in them, or which one contains one. 假设我有一个名为ISomething的接口,它是一个契约,我在包含我的应用程序的文件夹中有多个程序集,我不知道它们中有多少ISomething实现,或者哪一个包含一个。

If I create a host application in which I use MEF, and would like to get an ISomething, but only one, how does MEF decide which implementation it will give me? 如果我创建一个我使用MEF的主机应用程序,并希望得到一个ISomething,但只有一个,MEF如何决定它将给我哪个实现?

Or, similarly to a common IoC container, how do I tell it which implementation should it use? 或者,与常见的IoC容器类似,如何告诉它应该使用哪个实现?

Thanks in advance for your answers. 提前感谢您的回答。

See this blog post for a discussion of this issue and some of the options you have. 有关问题的讨论以及您拥有的一些选项,请参阅此博客文章 Also, Glenn Block has a blog post describing how to customize the container behavior with defaults . 此外,Glenn Block有一篇博客文章描述了如何使用默认值自定义容器行为

In the case of MEF, if you have many Exports that will satisfy an Import, you have two options: 对于MEF,如果您有许多将满足Import的Export,您有两个选择:

  1. Change your Import around to use [ImportMany] . 更改您的导入以使用[ImportMany] Decide, at runtime, which of the Imports to use for your contract, potentially just picking the first, or one at random. 在运行时确定要用于合同的哪个Imports,可能只是选择第一个,或随机选择一个。
  2. Use [ImportMany] in conjunction with Metadata in order to decide which Import to use. [ImportMany]元数据结合使用以确定要使用的Import。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM