使用 Unity 类型转换注入属性值
我正在使用 Unity Fluent API 在对象上注入属性值。我的程序从外部配置源(一个 xml 文件)获取属性值。
我面临的问题是,我尝试设置的属性是 Int32 类型,但从 xml 文件读取的值最初被转换为字符串。我需要做什么才能让 Unity 进行必要的类型转换?如果我在 Unity 配置文件中配置容器,它就可以工作,但是我不确定在代码中执行此操作时需要执行哪些进一步步骤。
这是我的代码:
using (IUnityContainer taskContainer = new UnityContainer())
{
taskContainer.RegisterType(typeof(ITask), jobType);
// Configure properties
foreach (PropInfo prop in jobConfig.Props)
{
// The next line raises an exception when setting an Int32 property
taskContainer.RegisterType(jobType, new InjectionProperty(prop.Name, prop.Value));
}
ITask myTask = taskContainer.Resolve<ITask>();
}
词汇表: * prop.Value 是 Object 类型。 * 被注入的属性是Int32类型。 * jobType 是 Type 类型并且之前已分配。 * PropInfo 是一个定制的帮助器类,用于访问配置信息。 * jobInfo 是类似帮助器类的实例。
该代码适用于将属性值注入字符串属性,但当我点击 Int32 属性时,出现异常:“DynamicTask 类型上的属性 RetryCount 的类型为 Int32,并且无法注入 String 类型的值”。 [显然,该属性的名称是 RetryCount,它位于名为 DynamicTask 的对象上]。
我猜测我需要执行一些前面的步骤来注册要注入的属性的类型,但我无法计算出语法,Unity 文档中有一些示例,但我不确定它们是否适用于我的情况。
如果有人能告诉我之前需要采取哪些步骤,我将不胜感激。
非常感谢。
I am using the Unity fluent API to inject property values on an object. My program obtains the value of the properties from an external configuration source, an xml file.
The problem I am facing is that the property I am attempting to set is of type Int32, but the value, having been read from an xml file, is initially cast as a string. What do I need to do to make Unity do the necessary type conversion? If I configure the container in a Unity config file, it works, however I'm not sure what further steps are required when doing this in code.
Here is my code:
using (IUnityContainer taskContainer = new UnityContainer())
{
taskContainer.RegisterType(typeof(ITask), jobType);
// Configure properties
foreach (PropInfo prop in jobConfig.Props)
{
// The next line raises an exception when setting an Int32 property
taskContainer.RegisterType(jobType, new InjectionProperty(prop.Name, prop.Value));
}
ITask myTask = taskContainer.Resolve<ITask>();
}
Glossary:
* prop.Value is of type Object.
* the property which is being injected is of type Int32.
* jobType is of type Type and has been assigned previously.
* PropInfo is a bespoke helper class for accessing config info.
* jobInfo is an instance of a similar helper class.
The code works well for injecting properties values to string properties but when I hit the Int32 property I get the exception: "The property RetryCount on type DynamicTask is of type Int32, and cannot be injected with a value of type String". [Obviously the name of the property is RetryCount and it is on an object named DynamicTask].
I am guessing that I need to do some preceding steps to register the type of the property being injected, but I cannot work out the syntax, there are some examples in the Unity documentation but I'm not sure if they apply to my situation.
I would be very grateful if someone could tell me what preceding steps I need to take.
Many thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Unity 的 API 不执行您要求的类型转换。我们使用 .NET TypeConverter 基础结构来处理 XML 端的转换,但 API 中通常不需要它,因此我们不需要对其进行任何特殊处理。
您提出的解决方案与其他解决方案一样好。
Unity's API doesn't do the kind of type conversion you're asking for. We use the .NET TypeConverter infrastructure to handle the conversion from the XML side, but it's typically not needed in the API so we don't have any special handling for it.
The solution you've come up with is as good as any other.