Skip to main content
 首页 » 编程设计

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

2024年02月23日22shanyou

我尝试使用 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 样式配置将键值对列表传递给对象。

请您参考如下方法:

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

<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: 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'. ...