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

发布于 2024-12-14 02:56:05 字数 2546 浏览 2 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-12-21 02:56:05

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

<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] 为必填项
类型 [System.Collections.Generic.IDictionary`2[[System.String,
mscorlib,版本=4.0.0.0,文化=中性,
PublicKeyToken = b77a5c561934e089],[System.String,mscorlib,
版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089]]]
对于属性“Params”。 ...

You have to specify the key- and value type for the dictionary:

<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>

Without these type definitions, spring tries to instantiate a non-generic dictionary, which results in the TypeMismatchException from your error message:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文