MEF:“可组合部件应包含至少一个 EXPORT”
来自官方 MEF 文档:
可组合部件应至少包含一个导出。
可组合部件要么显式添加到容器中,要么通过使用目录创建。
MEF 附带的默认目录通过导出属性来标识可组合部件。
这是否意味着在使用默认目录时,形成相同文档的此片段将不起作用,因为它不导出任何内容?
class Program
{
[Import]
public IMessageSender MessageSender { get; set; }
}
我很难相信参与MEF mix的所有班级都必须“把食物送到餐桌上”,即使他们只是想“消费”。
上面的 Program
是一个简单的示例:此类无法添加到 MEF mix 中。
其次,
如何将零件“显式添加到容器”?
例如 CompositionContainer 的文档 对我没有任何帮助。
提前致谢 扬
From the official MEF documentation:
A Composable Part should contain at least one export.
Composable Parts are either added to the container explicity or created through the use of catalogs.
The default catalogs that MEF ship with identify Composable Parts through the presence of an export attribute.
Does this mean that this snippet form the same documentation would not work, when using the default catalogs, since it doesn't export anything?
class Program
{
[Import]
public IMessageSender MessageSender { get; set; }
}
I find it hard to believe that all classes participating in the MEF mix, must all 'bring food to the table', even if they just want to 'consume'.
The Program
above being a simple example: there is nothing that this class could add to the MEF mix.
Secondly,
How are Parts being added "explicitly to the container"?
The documentation of e.g. CompositionContainer doesn't help me any further.
Thanks in advance
Jan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可组合部件是可以导入到另一个部件中的部件。此处的示例:
Program
不是可组合部分。它本身不会自动导入到另一个部分。您的IMessageSender
导出是可组合部分。您可以使用
CompositionBatch
向容器显式添加部件,它允许您向容器显式或通过扩展添加Export
和ComposablePart
实例您可以添加原始值:最终调用是将零件添加到容器中。
A composable part is a part that can be imported into another part. The example here:
Program
is not a composable part. It iself does not get automatically imported into another part. Your exports forIMessageSender
are composable parts.You can add parts explicity to the container using a
CompositionBatch
, which allows you to addExport
andComposablePart
instances to your container explicitly, or through extension you can add raw values:Where the final call is adding your parts to the container.
在您的示例中,程序必须调用
container.SatisfyImports();
才能检索IMessageSender
。此外,您可以在接口上使用继承导出,因此实现所述接口的所有内容都会自动导出。
请在此处查看我的代码: MEFMVVMCS.zip
in your example, the program would have to call
container.SatisfyImports();
to be able to retrieve theIMessageSender
.Also, you can use a inherited export on the interface, so everything that implements said interface is automatically exported.
See my code here: MEFMVVMCS.zip