MEF ComposeExportedValue 与导出属性

MEF ComposeExportedValue vs Export attribute

我很难理解为什么我们需要 ComposeExportedValue(objval) 而不是仅仅使用 [Export] 属性。

我在shell中创建了一个应用对象,这个应用对象需要注入到棱镜模块中。

public class ShellBootsrapper : MefBootstrapper
{

    [Export(typeof(IMyApplication))]
    public MyApplication myApp; 

    protected override DependencyObject CreateShell()
    {
        this.Container.ComposeExportedValue<IMyApplication>(myApp);
        return this.Container.GetExportedValue<Shell>();
    }

    protected override void ConfigureAggregateCatalog()
    {
       base.ConfigureAggregateCatalog();
       myApp = new MyApplication();
       this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()));
      this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Module1.Module1).Assembly));
}

}

模块 1 只有在我使用 ComposeExportedValue<IMyApplication>(myApp);

时才能导入
[ModuleExport(typeof(Module1))] 
public class Module1 : IModule
{

    private readonly IRegionManager regionManager;


    [Import]
    private IMyApplication myApp;

}

我希望 [导出] 就足够了,但显然不够?

编辑: 我从引导程序中删除了 public MyApplication myApp; 到 shell.xaml.cs(more sensible) 并且一切正常。我得出结论; MEF 合成正在进行中,导出根本不起作用。这就是为什么棱镜内部库使用 ComposeExportedValue(object val)

进行导出
protected virtual void RegisterBootstrapperProvidedTypes()
{
    this.Container.ComposeExportedValue<ILoggerFacade>(this.Logger);
    this.Container.ComposeExportedValue<IModuleCatalog>(this.ModuleCatalog);
    this.Container.ComposeExportedValue<IServiceLocator>(new MefServiceLocatorAdapter(this.Container));
    this.Container.ComposeExportedValue<AggregateCatalog>(this.AggregateCatalog);
}

我从引导程序中删除了 public MyApplication myApp; 到 shell.xaml.cs(更明智),一切正常。我得出结论;在引导程序中,MEF 合成正在进行中,导出根本不起作用。这就是为什么棱镜内部库使用 ComposeExportedValue(object val)

进行导出
protected virtual void RegisterBootstrapperProvidedTypes()
{
    this.Container.ComposeExportedValue<ILoggerFacade>(this.Logger);
    this.Container.ComposeExportedValue<IModuleCatalog>(this.ModuleCatalog);
    this.Container.ComposeExportedValue<IServiceLocator>(new MefServiceLocatorAdapter(this.Container));
    this.Container.ComposeExportedValue<AggregateCatalog>(this.AggregateCatalog);
}

我还发现 ComposeExportedValue 的目的之一是进行受控导出;即配置对象,设置属性等,然后将其导出。否则 MEF 只会导出创建一个实例。