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

发布于 2024-10-31 00:07:49 字数 406 浏览 0 评论 0原文

给定一个带有 [Import] 标签的任意已存在的对象,为了让 MEF 填充导入,我必须做什么手鼓舞?

许多博客文档似乎是针对 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?!?!
    }
}

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?

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).

AggregateCatalog _catalog;
CompositionContainer _container;

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱自由 2024-11-07 00:07:49

MEF 从目录中注册的已注册程序集(包括当前程序集)中的导出类型解析导入(通过属性或构造函数注入)以及它们自己的依赖项。

如果您想直接创建对象(使用 new 关键字),或者创建时导出尚未准备好,您可以使用容器为了满足对象的导入,使用:

_container.SatisfyImportsOnce(yourObject);

我已经整理了一个小场景来做到这一点。代码如下:

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
}

现在,只需使用 new Tests(new CompositionContainer()).Test(); 即可启动演示。

希望这有帮助:)

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).

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:

_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(); to start the demo.

Hope this helps :)

混浊又暗下来 2024-11-07 00:07:49
_container.ComposeParts(existingObjectWithImportTags);

ComposeParts 是您正在寻找的扩展方法。

它只是创建一个 CompositionBatch 并调用 AddPart(AttributedModelServices.CreatePart(attributedObject)),然后调用 _container.Compose(batch)。

_container.ComposeParts(existingObjectWithImportTags);

ComposeParts is an extension method that you are looking for.

It simply creates a CompositionBatch and calls AddPart(AttributedModelServices.CreatePart(attributedObject)) and then calls _container.Compose(batch).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文