MEF构造器注入 [英] MEF Constructor Injection

查看:99
本文介绍了MEF构造器注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图找出MEF的构造函数注入属性。我不知道我如何告诉它加载构造函数的参数。



这是我正在加载的属性

  [ImportMany(typeof(BUsers))] 
public IEnumerable< BUsers> LoadBUsers {get;组;

以下是我用于导入程序集的代码。

  try 
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()));
catalog.Catalogs.Add(new DirectoryCatalog(DI));
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}

这是我正在加载的课程

  [Serializable] 
[Export(typeof(BUsers))]
public class EditProfile:BUsers
{
[ImportingConstructor]
public EditProfile(string Method,string Version)
{
Version =2;
Action =编辑;
TypeName =EditProfile;
}


解决方案

当您使用ImportingConstructor属性,构造函数的参数变为导入。默认情况下,导入的内容(合同名称)是根据您导入的参数或属性的类型而定。所以在这种情况下,您的导入的合同类型是字符串,第一个和第二个参数之间没有真正的区别。



看起来你正在尝试使用导入提供配置值,这不一定是为它设计的。要使其执行所需的操作,您应该覆盖每个参数的合同名称,如下所示:

  [ImportingConstructor ] 
public EditProfile([Import(Method)] string方法,[Import(Version)] string Version)
{}

然后,您需要在容器中导入Method和Version。一个方法是直接添加它们:

  var container = new CompositionContainer(catalog); 
container.ComposeExportedValue(Method,MethodValue);
container.ComposeExportedValue(Version,2.0);
container.ComposeParts(this);

(请注意,ComposeExportedValue实际上是一个在静态AttributedModelServices类上定义的扩展方法。)



如果要从某种配置文件读取这些值,您可以创建自己的导出提供程序,它读取配置并将其中的值作为导出到容器。 / p>

另一个处理方式的方法就是导入一个接口,通过名称提供对配置值的访问,并从构造函数的正文中获取所需的值。


I'm trying to figure out MEF's Constructor Injection attribute. I have no idea how I tell it to load the constructor's parameters.

This is the property I'm trying to load

[ImportMany(typeof(BUsers))]
public IEnumerable<BUsers> LoadBUsers { get; set; }

Here is the code I'm using to import the assemblies.

try
{
    var catalog = new AggregateCatalog();
    catalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()));
    catalog.Catalogs.Add(new DirectoryCatalog("DI")); 
    var container = new CompositionContainer(catalog);
    container.ComposeParts(this);
}

Here is the class I'm trying to load

[Serializable]
[Export(typeof(BUsers))]
public class EditProfile : BUsers
{
    [ImportingConstructor]
    public EditProfile(string Method, string Version)
    {            
        Version = "2";
        Action = "Edit";
        TypeName = "EditProfile";
    }

解决方案

When you use the ImportingConstructor attribute, the parameters to the constructor become imports. By default, what you are importing (the contract name) is based on the type of the parameter or property that your are importing into. So in this case the contract type for both your imports is string, and there's no real difference between the first and second parameter.

It looks like you are trying to use imports to supply configuration values, which isn't necessarily what it was designed for. To get it to do what you want, you should override the contract name for each of the parameters, like this:

[ImportingConstructor]
public EditProfile([Import("Method")] string Method, [Import("Version")] string Version)
{ }

Then you need exports for Method and Version in your container. One way to do this is just to add them directly:

var container = new CompositionContainer(catalog);
container.ComposeExportedValue("Method", "MethodValue");
container.ComposeExportedValue("Version", "2.0");
container.ComposeParts(this);

(Note that ComposeExportedValue is actually an extension method defined on the static AttributedModelServices class.)

If you want to read these values from a configuration file of some sort, you could create your own export provider which reads the configuration and provides the values in it as exports to the container.

An alternative way to handle this would be to just import an interface that provides access to the configuration values by name, and get the values you need from the body of the constructor.

这篇关于MEF构造器注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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