gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:07:08 25 4
gpt4 key购买 nike

我尝试使用 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'. ...

关于c# - 在 Spring.Net 中初始化 IDictionary 对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8027367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com