简体   繁体   English

使用Unity注入属性值-类型转换

[英]Injecting property values with Unity - type conversion

I am using the Unity fluent API to inject property values on an object. 我正在使用Unity fluent API在对象上注入属性值。 My program obtains the value of the properties from an external configuration source, an xml file. 我的程序从一个外部配置源(一个xml文件)获取属性的值。

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. 我面临的问题是,我要设置的属性的类型为Int32,但是从xml文件读取的值最初被转换为字符串。 What do I need to do to make Unity do the necessary type conversion? 我需要做什么才能使Unity进行必要的类型转换? 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. 如果我在Unity配置文件中配置容器,则可以使用该容器,但是我不确定在代码中执行此操作时还需要执行哪些进一步的步骤。

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. 词汇表:* prop.Value是Object类型。 * the property which is being injected is of type Int32. *注入的属性为Int32类型。 * jobType is of type Type and has been assigned previously. * jobType是Type类型,并且先前已分配。 * PropInfo is a bespoke helper class for accessing config info. * PropInfo是用于访问配置信息的定制帮助程序类。 * jobInfo is an instance of a similar helper class. * jobInfo是类似的帮助程序类的实例。

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". 该代码可以很好地将属性值注入到字符串属性中,但是当我点击Int32属性时,我得到了一个例外:“ DynamicTask类型的RetryCount属性为Int32类型,不能注入String类型的值”。 [Obviously the name of the property is RetryCount and it is on an object named DynamicTask]. [显然,该属性的名称是RetryCount,并且在名为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. 我猜想我需要执行一些前面的步骤来注册要注入的属性的类型,但是我无法确定语法,Unity文档中有一些示例,但是我不确定它们是否适用于我的情况。

I would be very grateful if someone could tell me what preceding steps I need to take. 如果有人可以告诉我我需要采取哪些先前的步骤,我将不胜感激。

Many thanks in advance. 提前谢谢了。

Unity's API doesn't do the kind of type conversion you're asking for. Unity的API不会执行您要求的类型转换。 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. 我们使用.NET TypeConverter基础结构从XML端处理转换,但是API中通常不需要它,因此我们对此没有任何特殊处理。

The solution you've come up with is as good as any other. 您提供的解决方案与其他解决方案一样出色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM