简体   繁体   English

使用MEF满足现有对象的导入

[英]Satisfying imports for an existing object using MEF

Given an arbitrary already existing object which is attributed with [Import] tags, what's the tambourine dance I have to do in order to get MEF to fill in the imports? 鉴于任意已存在的对象归属于[导入]标签,为了让MEF填写进口,我必须做什么手鼓舞?

Much of the blog documentation seems to be built against preview versions of MEF and don't work anymore - I'm using the released one that's part of .NET 4.0 (or alternatively, MEF 2.0 Preview 3). 许多博客文档似乎是针对MEF的预览版本而构建的,并且不再起作用了 - 我正在使用已发布的一个.NET 4.0(或者MEF 2.0 Preview 3)。

AggregateCatalog _catalog;
CompositionContainer _container;

public void Composify(object existingObjectWithImportTags)
{
    lock(_container) {
        var batch = new CompositionBatch();

        // What do I do now?!?!
    }
}

MEF resolves Imports (through property or constructor injection), along whith their own dependencies, from exported types in the registered assemblies registered in the catalog (including the current assembly). MEF根据目录中注册的已注册程序集中的导出类型(包括当前程序集)解析Imports (通过属性或构造函数注入)及其自身的依赖项。

If you want to create an object directly (using the new keyword), or in case the export wasn't ready at the time of the creation, you can use the container to satisfy the imports of an object, using: 如果要直接创建对象(使用new关键字),或者在创建时导出尚未就绪,则可以使用容器来满足对象的导入 ,使用:

_container.SatisfyImportsOnce(yourObject);

I've put together a little scenario doing just that. 我做了一个小场景就是这么做的。 here's the code: 这是代码:

public class Demo
{
    private readonly CompositionContainer _container;

    [Import]
    public IInterface Dependency { get; set; }

    public Demo(CompositionContainer container)
    {
        _container = container;
    }

    public void Test()
    {

        //no exported value, so the next line would cause an excaption
        //var value=_container.GetExportedValue<IInterface>();

        var myClass = new MyClass(_container);

        //exporting the needed dependency
        myClass.Export();

        _container.SatisfyImportsOnce(this);

        //now you can retrieve the type safely since it's been "exported"
        var newValue = _container.GetExportedValue<IInterface>();
    }
}

public interface IInterface
{
    string Name { get; set; }
}

[Export(typeof(IInterface))]
public class MyClass:IInterface
{
    private readonly CompositionContainer _container;

    public MyClass()
    {

    }
    public MyClass(CompositionContainer container)
    {
        _container = container;
    }

    #region Implementation of IInterface

    public string Name { get; set; }

    public void Export()
    {
        _container.ComposeExportedValue<IInterface>(new MyClass());
    }

    #endregion
}

Now, just use new Tests(new CompositionContainer()).Test(); 现在,只需使用new Tests(new CompositionContainer()).Test(); to start the demo. 开始演示。

Hope this helps :) 希望这可以帮助 :)

_container.ComposeParts(existingObjectWithImportTags);

ComposeParts is an extension method that you are looking for. ComposeParts是您正在寻找的扩展方法。

It simply creates a CompositionBatch and calls AddPart(AttributedModelServices.CreatePart(attributedObject)) and then calls _container.Compose(batch). 它只是创建一个CompositionBatch并调用AddPart(AttributedModelServices.CreatePart(attributesObject)),然后调用_container.Compose(batch)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM