在 Spring.Net 中初始化 IDictionary 对象属性
我尝试使用 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 样式配置将键+值对列表传递给对象。
I try to initialize typed IDictionary
object property with xml Spring.net declaration, but exception occurs.
Here is class definition:
public class MyObjectClass
{
public IDictionary<string,string> Params { get; set; }
}
Corresponding fragment of spring xml configuration:
<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>
Unfortunately it doesn't work. Exception is thrown during spring initialization:
[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
I'm focused on passing list of key+value pairs to object with usage of xml style configuration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须指定字典的键和值类型:
如果没有这些类型定义,spring 会尝试实例化一个非泛型字典,这会导致错误消息中的
TypeMismatchException
:You have to specify the key- and value type for the dictionary:
Without these type definitions, spring tries to instantiate a non-generic dictionary, which results in the
TypeMismatchException
from your error message: