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

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

问题描述

我试图找出MEF的构造器注入属性。我不知道我该怎么告诉它加载构造函数的参数。



这是我试图加载


$ B $属性b

  [ImportMany(typeof运算(BUsers))] 
公开的IEnumerable< BUsers> LoadBUsers {搞定;组; }

下面是我使用导入的组件的代码。

 
{
变种目录=新AggregateCatalog();
catalog.Catalogs.Add(新AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()));
catalog.Catalogs.Add(新DirectoryCatalog(DI));
变种集装箱=新CompositionContainer中(目录);
container.ComposeParts(本);
}

下面是我试图加载

$ B类
$ b

  [Serializable接口] 
[导出(typeof运算(BUsers))]
公共类EditProfile:BUsers
{
[ImportingConstructor]
公共EditProfile(字符串的方法,弦乐版)
{

版本=2;
=行动编辑;
类型名=EditProfile;
}


解决方案

当您使用ImportingConstructor属性,该参数的构造变得进口。默认情况下,你要导入(合同名称)是基于您导入到参数或属性的类型。因此,在这种情况下,为您的进口合同类型为字符串,并有第一和第二个参数之间没有真正的区别。



看起来你试图使用进口供给配置值,这不一定是什么它被设计为。为了得到它做你想要什么,你应该为每一个参数的重写合同的名称,如:

  [ImportingConstructor ] 
公共EditProfile([导入(办法)的字符串的方法,[导入(版本)的字符串版)
{}

然后,你需要在你的容器的方法和版本的出口。这样做的一个方法是把它们直接添加:

  VAR集装箱=​​新CompositionContainer中(目录); 
container.ComposeExportedValue(方法,MethodValue);
container.ComposeExportedValue(版本,2.0);
container.ComposeParts(本);



(注意ComposeExportedValue实际上是对静态AttributedModelServices类中定义的扩展方法。)



如果你想阅读从某种类型的配置文件中的这些值,你可以创建自己的出口提供商,读取配置,并作为出口容器提供的值。



来处理这将是刚刚导入提供按名称访问配置值的接口,让你从构造体所需要的价值观的另一种方法。


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天全站免登陆