MEF异常” “源提供者”必须设置。
我正在使用新的 系统。 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CatalogExportProvider 需要引用回容器。以下代码应该可以工作:
当您将目录传递给构造函数时,容器会自动执行此操作。我认为通常没有太多理由手动创建 CatalogExportProvider。
CatalogExportProvider 需要对容器的引用的原因是,目录中可能存在包含导入的部分,需要由容器所连接的其他导出提供程序来满足。
The CatalogExportProvider needs a reference back to the container. The following code should work:
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.