Unity:在 xml 配置文件中将参数传递给自定义生命周期构造函数
我这样编写了 CustomLifetimeManager:
public class CustomLifetimeManager <T> : LifetimeManager
{
private readonly string _arg;
public CustomLifetimeManager(string arg)
{
_arg = arg;
}
}
现在,可以轻松地以编程方式配置容器,但是如何将其添加到配置文件中,如下所示?
<type type="myTime"
mapTo="myImpl">
<lifetime type="CustomLifetimeManager"/>
</type>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要添加第二个类:TypeConverter。该类负责获取字符串并将其转换为您想要的任何类型。一旦实现它,您就可以在配置文件中执行类似的操作:
并且从那里它应该可以正常工作(假设配置可以像任何其他类型一样找到类型转换器)。
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:
and from there it should just work (assuming the config can find the type converter like any other type).