从导入的属性中检索信息时出现问题。调用 .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 {...}
任何指针表示赞赏,非常感谢。