MEF异常” “源提供者”必须设置。

发布于 2024-08-16 03:14:03 字数 1002 浏览 3 评论 0原文

我正在使用新的 系统。 .NET 4.0 beta 2 中的 ComponentModel.Composition 命名空间,也称为托管扩展性框架。

我使用以下 C# 示例,其中 Monkey 导入 Banana

public interface IBanana 
{ 
}

[Export(typeof(IBanana))]
public class Banana : IBanana 
{ 
} 

public class Monkey
{
    [Import(typeof(IBanana))]
    public IBanana Banana { get; set; }
}

但是,当我尝试按如下方式组合猴子时,我收到 InvalidOperationException消息“此对象尚未初始化 - 必须设置属性“SourceProvider”。”:

var exportProvider = new CatalogExportProvider(new TypeCatalog(typeof(Banana)));
var container = new CompositionContainer(exportProvider);
var monkey = new Monkey();
container.ComposeParts(monkey);

我在这里缺少什么?我知道我可以直接传递目录,而无需将其包装在 CatelogExportProvider 中,但上面的方法不应该也有效吗?

I am playing around with the new System.ComponentModel.Composition namespace in .NET 4.0 beta 2, also known as the Managed Extensibility Framework.

I use the following C# example where a Monkey imports a Banana:

public interface IBanana 
{ 
}

[Export(typeof(IBanana))]
public class Banana : IBanana 
{ 
} 

public class Monkey
{
    [Import(typeof(IBanana))]
    public IBanana Banana { get; set; }
}

However, when I try to compose the monkey as follows then I get an InvalidOperationException with the message "This object has not been initialized - the property 'SourceProvider' must be set.":

var exportProvider = new CatalogExportProvider(new TypeCatalog(typeof(Banana)));
var container = new CompositionContainer(exportProvider);
var monkey = new Monkey();
container.ComposeParts(monkey);

What am I missing here? I am aware that I can pass the catalog directly without wrapping it in a CatelogExportProvider, but shouldn't the above also work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

如此安好 2024-08-23 03:14:03

CatalogExportProvider 需要引用回容器。以下代码应该可以工作:

var exportProvider = new CatalogExportProvider(new TypeCatalog(typeof(Banana)));
var container = new CompositionContainer(exportProvider);
exportProvider.SourceProvider = container;
var monkey = new Monkey();
container.ComposeParts(monkey);

当您将目录传递给构造函数时,容器会自动执行此操作。我认为通常没有太多理由手动创建 CatalogExportProvider。

CatalogExportProvider 需要对容器的引用的原因是,目录中可能存在包含导入的部分,需要由容器所连接的其他导出提供程序来满足。

The CatalogExportProvider needs a reference back to the container. The following code should work:

var exportProvider = new CatalogExportProvider(new TypeCatalog(typeof(Banana)));
var container = new CompositionContainer(exportProvider);
exportProvider.SourceProvider = container;
var monkey = new Monkey();
container.ComposeParts(monkey);

The container does this automatically when you pass a catalog into the constructor. I don't think there's often much of a reason to create the CatalogExportProvider manually.

The reason the CatalogExportProvider needs a reference to the container is that there may be parts in the catalog with imports that need to be satisfied by other export providers that the container is hooked up to.

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