在子对象中向字典添加条目 [英] Add entry to dictionary in child object

查看:159
本文介绍了在子对象中向字典添加条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在子对象中添加条目到父对象中定义的字典?

How is it possible to add entry to dictionary, defined in parent object, from the child object?

* UPDATED
我按以下方式编辑我的配置:

*UPDATED I edit my config as follows:

<object id="ParentType" singleton="false" type="SomeType">
  <property name="FieldNameMap">
   <dictionary key-type="string"  value-type="string" >
     <entry key="Title" value="TitleName"/>     
    </dictionary>
  </property>
</object>

<object id="ChildType" singleton="false" type="SomeType" parent="ParentType">
  <property name="FieldNameMap">
   <dictionary merge="true">
     <!-- this entry should be added to, not replace the whole Dictionary -->
     <entry key="Position" value="PositionName"/>     
    </dictionary>
  </property>
</object>

但不幸的是,现在它抛出了异常:

but, unfortunately, it now throws cast exception:


无法将类型
的属性值转换为必需类型
[System.Collections.Generic.IDictionary`2 [[System.Collections.Specialized.HybridDictionary] .String,
],[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,
PublicKeyToken = b77a5c561934e089]]

Cannot convert property value of type [System.Collections.Specialized.HybridDictionary] to required type [System.Collections.Generic.IDictionary`2[[System.String, ],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]


推荐答案

我没有尝试过,但根据文档,spring.net将使用集合的添加方法添加项目,如果集合本身公开为只读属性。

I haven't tried this myself, but according to the docs, spring.net will use the Add method of the collection to add items, if the collection itself is exposed as a readonly property.

因此,可能可以使用对象定义继承来实现您描述的行为。 Config将类似于:

So it might be possible to use object definition inheritance to achieve the behavior you describe. Config will be something like:

<object id="ParentType" singleton="false" type="SomeType"
        abstract="true">
  <property name="FieldNameMap">
   <dictionary>
     <entry key="Title" value="TitleName"/>     
    </dictionary>
  </property>
</object>

<object id="ChildType" singleton="false" type="SomeType" 
        parent="ParentType">
  <property name="FieldNameMap">
   <dictionary>
     <!-- this entry should be added to, not replace the whole Dictionary -->
     <entry key="Position" value="PositionName"/>     
    </dictionary>
  </property>
</object>

这只会在 FieldNameMap readonly属性。

This will only work if FieldNameMap is a readonly property.

这篇关于在子对象中向字典添加条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆