[英]MEF Imported property is always null
In a minimal application I'm using to understand how MEF works I have the following class and interface,在我用来了解 MEF 工作原理的最小应用程序中,我有以下 class 和接口,
[Export(typeof(IAnimal))]
public class Animal : IAnimal
{
public string Name { get; set; }
public string GetAnimalName()
{
Name = "cat";
return Name;
}
}
internal interface IAnimal
{
string GetAnimalName();
}
In another Class Outside I'm trying to use MEF property injection to get an instant of the exported IAnimal as below,在另一个 Class 之外,我正在尝试使用 MEF 属性注入来获取导出的 IAnimal 的瞬间,如下所示,
[Import(typeof(IAnimal))]
public IAnimal AnimalProperty{ get; set; }
This property "AnimalProperty" is always null and I can't understand where the missing piece is.此属性“AnimalProperty”始终为 null,我不明白缺少的部分在哪里。 Any thoughts on what I might be missing here?
关于我可能在这里遗漏的任何想法?
Without more context I cannot say for sure what the problem is but here are a few things that I can think of that may be what is happening:如果没有更多上下文,我无法确定问题出在哪里,但这里有一些我能想到的可能正在发生的事情:
new()
keyword, instead of telling Dependency Injection to inject it for you.new()
关键字创建使用 IAnimal 属性的外部 class 的实例,而不是告诉依赖注入为您注入它。 The Dependency Injection Container will NOT inject the dependencies needed for the class if the class itself was not created by the DI Container.var outsideClassInstance = compositionCointainer.GetExportedValue<OutsideClass>();
var outsideClassInstance = compositionCointainer.GetExportedValue<OutsideClass>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.