MEF:“可组合部件应包含至少一个 EXPORT”

发布于 2024-11-19 17:53:36 字数 789 浏览 7 评论 0原文

来自官方 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 技术交流群。

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

发布评论

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

评论(2

杀手六號 2024-11-26 17:53:36

可组合部件是可以导入到另一个部件中的部件。此处的示例:

class Program
{
    [Import]
    public IMessageSender MessageSender { get; set; }
}

Program 不是可组合部分。它本身不会自动导入到另一个部分。您的 IMessageSender 导出是可组合部分。

您可以使用 CompositionBatch 向容器显式添加部件,它允许您向容器显式或通过扩展添加 ExportComposablePart 实例您可以添加原始值:

var user = new User() { Name = "Matt" };
var batch = new CompositionBatch();
batch.AddExportedValue(user);

CompositionContainer.Compose(batch);

最终调用是将零件添加到容器中。

A composable part is a part that can be imported into another part. The example here:

class Program
{
    [Import]
    public IMessageSender MessageSender { get; set; }
}

Program is not a composable part. It iself does not get automatically imported into another part. Your exports for IMessageSender are composable parts.

You can add parts explicity to the container using a CompositionBatch, which allows you to add Export and ComposablePart instances to your container explicitly, or through extension you can add raw values:

var user = new User() { Name = "Matt" };
var batch = new CompositionBatch();
batch.AddExportedValue(user);

CompositionContainer.Compose(batch);

Where the final call is adding your parts to the container.

森林散布 2024-11-26 17:53:36

在您的示例中,程序必须调用 container.SatisfyImports(); 才能检索 IMessageSender

此外,您可以在接口上使用继承导出,因此实现所述接口的所有内容都会自动导出。

[InheritedExport]
public interface IMessageSender
{
}

请在此处查看我的代码: MEFMVVMCS.zip

in your example, the program would have to call container.SatisfyImports(); to be able to retrieve the IMessageSender.

Also, you can use a inherited export on the interface, so everything that implements said interface is automatically exported.

[InheritedExport]
public interface IMessageSender
{
}

See my code here: MEFMVVMCS.zip

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