0
从导入的属性中检索信息时遇到问题。在调用.ComposeParts()后,该属性保持为空,但组成正常,因为之后我可以调用.GetExportedValues(),并获取所需的实例。下面是代码:MEF属性注入不会发生
引导程序做组合物
[Export]
public class Bootstrapper
{
public void Run()
{
doComposition();
}
private void doComposition()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog("./Applications"));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Loader).Assembly));
Container = new CompositionContainer(catalog);
// Apps = Container.GetExportedValues<IApplication>(); - this gets me the IApplication(s), but I dont understand why Apps isn't injected automatically
Container.ComposeParts(catalog);
IEnumerable<IApplication> app = Container.GetExportedValues<IApplication>();
}
public CompositionContainer Container { get; set; }
private IEnumerable<IApplication> apps;
[ImportMany(typeof(IApplication))]
public IEnumerable<IApplication> Apps
{
get { return apps; }
set
{
apps = value;
}
}
实施IApplication
[Export(typeof(IApplication))]
public class MDFApplication : IApplication {...}
任何指针中的一个类的签名被理解的那样,非常感谢。