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 {...}
任何指針中的一個類的簽名被理解的那樣,非常感謝。