在Spring.Net中初始化IDictionary对象属性

时间:2011-11-06 13:41:44

标签: c# spring.net

我尝试使用xml Spring.net声明初始化类型化IDictionary对象属性,但发生异常。

这是类定义:

public class MyObjectClass
{
   public IDictionary<string,string> Params { get; set; }
}

spring xml配置的相应片段:

<object id="MyObject" type="MyObjectClass, MyAssembly">
    <property name="Params">
        <dictionary>
            <entry key="Error" value="1"/>
            <entry key="Warning" value="2"/>
            <entry key="Information" value="4"/>
         </dictionary>
    </property>
</object>

不幸的是它不起作用。在spring初始化期间抛出异常:

[Spring.Core.TypeMismatchException: Cannot convert property value of type [System.Collections.Specialized.HybridDictionary] to required type [System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]] for property 'Params'., Inner Exception: Spring.Core.TypeMismatchException: Cannot convert property value of type [System.Collections.Specialized.HybridDictionary] to required type [System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]] for property 'Params'.
  in Spring.Core.TypeConversion.TypeConversionUtils.ConvertValueIfNecessary(Type requiredType, Object newValue, String propertyName) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Core\TypeConversion\TypeConversionUtils.cs:175]
    Source=Spring.Core
    ExceptionCount=1
    StackTrace:
      in Spring.Objects.ObjectWrapper.SetPropertyValues(IPropertyValues propertyValues, Boolean ignoreUnknown) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\ObjectWrapper.cs:377
      in Spring.Objects.ObjectWrapper.SetPropertyValues(IPropertyValues pvs) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\ObjectWrapper.cs:305
      in Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.ApplyPropertyValues(String name, RootObjectDefinition definition, IObjectWrapper wrapper, IPropertyValues properties) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractAutowireCapableObjectFactory.cs:384

我专注于使用xml样式配置将键+值对列表传递给对象。

1 个答案:

答案 0 :(得分:4)

您必须指定字典的键值和值类型:

<object id="MyObject" type="q8027367.MyObjectClass, q8027367">
  <property name="Params">
    <!-- Note the type definitions: -->
    <dictionary key-type="string" value-type="string">
      <entry key="Error" value="1"/>
      <entry key="Warning" value="2"/>
      <entry key="Information" value="4"/>
    </dictionary>
  </property>
</object>

如果没有这些类型定义,spring会尝试实例化非泛型字典,从而导致出现错误消息中的TypeMismatchException

  

[Spring.Core.TypeMismatchException:无法转换属性值   输入[System.Collections.Specialized.HybridDictionary]到必需   type [System.Collections.Generic.IDictionary`2 [[System.String,   mscorlib,版本= 4.0.0.0,文化=中性,   PublicKeyToken = b77a5c561934e089],[System.String,mscorlib,   Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]]   物业'Params'。 ...