统一:将参数传递给定制一生的构造,在xml配置文件 [英] Unity: pass parameters to custom lifetime constructor, in xml configuration file

查看:184
本文介绍了统一:将参数传递给定制一生的构造,在xml配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写我CustomLifetimeManager是这样的:

 公共类CustomLifetimeManager< T> :LifetimeManager 
{
私人只读字符串_arg;

公共CustomLifetimeManager(字符串ARG)
{
_arg = ARG;
}
}

现在,它的工作原理很容易配置容器编程,但?如何在配置文件中像下面



添加

 <输入类型=数值指明MyTime
mapTo =myImpl >
<终身TYPE =CustomLifetimeManager/>
< /类型>


解决方案

您需要添加第二个等级:A类型转换器。该类负责获取一个字符串,并把它变成任何你想要的类型。一旦你实现它,你就可以做这样的事情在你的配置文件:

 <注册类型=MyType的mapTo MyImpl> 
<终身的TypeConverter =CustomLifetimeManagerConverterVALUE =ARG/>
< /注册>

和从那里它应该只是工作(假设配置可以找到类型转换器就像任何其他类型)


I wrote my CustomLifetimeManager like this:

public class CustomLifetimeManager <T> : LifetimeManager
{
    private readonly string _arg;

    public CustomLifetimeManager(string arg)
    {
      _arg = arg;
    }
}

Now, it works easy configuring the container programmatically, but how add it in configuration file like the following?

<type type="myTime"
      mapTo="myImpl">
      <lifetime type="CustomLifetimeManager"/>
</type>

解决方案

You need to add a second class: A TypeConverter. This class is responsible for taking a string and turning it into whatever type you want. Once you implement it, you can then do something like this in your config file:

<register type="MyType" mapTo"MyImpl">
  <lifetime typeConverter="CustomLifetimeManagerConverter" value="arg" />
</register>

and from there it should just work (assuming the config can find the type converter like any other type).

这篇关于统一:将参数传递给定制一生的构造,在xml配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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