【发布时间】:2012-02-01 11:16:24
【问题描述】:
从导入的属性中检索信息时遇到问题。调用 .ComposeParts() 后,该属性仍然为空,但组合正常,因为之后我可以调用 .GetExportedValues() 并获得所需的实例。代码如下:
引导程序进行组合
[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 {...}
不胜感激,万分感谢。
【问题讨论】:
标签: c# dependency-injection mef property-injection