MEF:如果我有多个出口,但只需要一个导入什么?多个、我有、但只、MEF

2023-09-03 12:13:57 作者:多跳跳能长高

我想环绕MEF我的脑海里。 有一件事我不明白。

I'm trying to wrap my mind around MEF. There is one thing I don't understand.

假设我有一个接口,命名为ISomething,这是一个合同,我有一个以上的组件,包含我的应用程序文件夹中,而且我不知道ISomething实现多少身在其中,或者是一个含有之一。

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.

如果我创造,我使用MEF,并希望得到一个ISomething,但只有一个主机应用程序,如何MEF决定哪些实现它会给我?

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?

或者,类似于常见的IoC容器,我怎么告诉它应该是使用哪种实现?

Or, similarly to a common IoC container, how do I tell it which implementation should it use?

在此先感谢您的回答。

推荐答案

请参阅this博客文章对于这一问题,一些你拥有的各种选择进行讨论。此外,格伦座有一篇博客文章中描述了如何customize容器的行为与默认。

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.