MEF:如何手动配置导出为一个合同执行 [英] MEF: How to manually configure Export for a contract implementation

查看:139
本文介绍了MEF:如何手动配置导出为一个合同执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MEF的CompositionContainer中,合同(说的IFoo)及模块(棱镜模块,但它并没有多大意义在这里,只是一些组件)。 我想在我的模块注册合同的执行情况(FooImpl)。

I have a MEF's CompositionContainer, a contract (say IFoo) and a module (Prism module but it doesn't matter much here, just some component). I want in my module to register the contract's implementation (FooImpl).

如果这是团结我愿意做这样的:

If it's Unity I'd do like this:

unity.RegisterType<IFoo, FooImpl>().

这就是全部。 随着MEF我有疑惑。我必须标记我的实现与ExportAttribute,但导致它会自动导出。我想通过自己来管理这个。 看看在code:

That's all. With MEF I've puzzled. I have to mark my implementation with ExportAttribute, but that lead to it'll be exported automatically. I want to manage this by myself. Take a look at the code:

class MyModule: IModule {
  private CompositionContainer m_container;
  public MyModule(CompositionContainer container) {
    m_container = container;
  }
  public void Initialize() {
    ??? I want something like m_container.CreateExport<IFoo, FooImpl>()
  }
}

public interface IFoo {}
public class FooImpl : IFoo {
    //[ImportingConstructor]
    public FooImpl(ISomeService svc) {}
}

在初始化我想manualy的导出的FooImpl因为IFoo的不是FooImpl类依靠ExportAttribute合同。 我明白,我只是创建一个实例FooImpl(在MyModule.Initialize以上),但 FooImpl对其他组件/服务构造函数的依赖,我想,他们要在创建解决。

In Initialize I want manualy export FooImpl as IFoo contract not relying on ExportAttribute on FooImpl class. I understand that I just can create an instance FooImpl (in MyModule.Initialize above), but FooImpl has constructor dependencies on other component/services and I want they to be resolved on creation.

所以也许我应该问:如何手动添加具有CompositionContainer中实例和合同出口?然后以某种方式纪念这个出口,因为它有ImportingConstructorAttribute?

So probably I should ask: how to manually add export having a CompositionContainer instance and a contract? And then mark this export somehow as it has ImportingConstructorAttribute?

推荐答案

这不是如何MEF工作在默认情况下。但是,您可以创建自己的 ExportProvider 的实现,它这样做,并把它传递给 CompositionContainer中的构造。

That's not how MEF works by default. However, you can create your own ExportProvider implementation which does this, and pass it to the CompositionContainer constructor.

借助 MEF的contrib 项目具有的<一个几个例子href="http://mefcontrib.$c$cplex.com/wikipage?title=Documentation%20%26%20Features&referringTitle=Home"相对=nofollow>出口提供者实现的。该<一href="http://mefcontrib.$c$cplex.com/wikipage?title=Provider%20Model%20Fluent%20Definition%20Provider"相对=nofollow>流利的定义提供商可能是最接近你的描述。还有一个出口供应商,提供具有统一的集成。

The MEF contrib project has a few examples of export provider implementations. The Fluent Definition Provider is probably the closest to what you describe. There is also an export provider which provides integration with unity.

这篇关于MEF:如何手动配置导出为一个合同执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆