如何使用MEF依赖注入提供程序? [英] How do I dependency inject providers using MEF?

查看:107
本文介绍了如何使用MEF依赖注入提供程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为可以编写以下代码:



I thought it would be possible to write the following code:

[Export]
public class ClassA
{
}

public class ClassB
{
  private readonly Func<ClassA> classAProvider;

  [ImportingConstructor]
  public ClassB(Func<ClassA> classAProvider)
  {
    this.classAProvider = classAProvider;
  }

  public ClassA CreateClassA()
  {
    return classAProvider();
  }
}





能帮我弄清楚如何为<$ c注入提供商$ c> ClassA 进入 ClassB ?我注意到 ExportFactory ,但我不喜欢我必须处理它的事实。



Can you please help me figure out how I can inject a provider for ClassA into ClassB? I have noticed ExportFactory, but I don''t like the fact that I have to dispose it.

推荐答案

请看一下,这里给出了完美的例子: http://blogs.msdn.com/b/brada/archive/2008/09/29/simple-introduction-to-composite-applications-with-the-managed -extensions-framework.aspx [ ^ ]

http://davesbox.com/archive/2008/06/05/dependency-injection-in- the-managed-extensibility-framework-simple-injection.aspx [ ^ ]
Please take a look, perfect example is given over here :http://blogs.msdn.com/b/brada/archive/2008/09/29/simple-introduction-to-composite-applications-with-the-managed-extensions-framework.aspx[^]
http://davesbox.com/archive/2008/06/05/dependency-injection-in-the-managed-extensibility-framework-simple-injection.aspx[^]


这篇关于如何使用MEF依赖注入提供程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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