Spring 在子对象中将条目添加到字典
如何从子对象向字典添加在父对象中定义的条目 *已更新 我编辑我的配置如下:Spring 在子对象中将条目添加到字典,spring,configuration,configuration-files,spring.net,Spring,Configuration,Configuration Files,Spring.net,如何从子对象向字典添加在父对象中定义的条目 *已更新 我编辑我的配置如下: <object id="ParentType" singleton="false" type="SomeType"> <property name="FieldNameMap"> <dictionary key-type="string" value-type="string" > <entry key="Title" value="TitleName"/&
<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>
但不幸的是,它现在抛出强制转换异常:
无法转换类型的属性值
[System.Collections.Specialized.HybridDictionary]到所需类型
[System.Collections.Generic.IDictionary`2[[System.String,
],[System.String,mscorlib,版本=4.0.0.0,区域性=中性,
PublicKeyToken=b77a5c561934e089]]
我自己没有尝试过,但是,如果集合本身作为只读属性公开,spring.net将使用集合的
Add
方法来添加项
因此,可以使用来实现您描述的行为。配置将类似于:
<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
是只读属性时,这才有效。我自己没有尝试过,但是,如果集合本身作为只读属性公开,spring.net将使用集合的Add
方法添加项
因此,可以使用来实现您描述的行为。配置将类似于:
<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
是只读属性时,这才有效。正确,甚至更好(使用Spring 1.3+):您现在可以合并非只读列表、字典、名称值和集合集(请参见5.3.2.6.集合合并)。问题是,它没有按预期工作,请参阅更新的QFixed问题,created pull request:您问题中的子对象定义没有引用父对象;您是否尝试过
。抱歉,这是Q中的一个输入错误。更正。更正,甚至更好(使用Spring 1.3+):您现在可以合并非只读列表、字典、名称值和集合(请参见5.3.2.6.集合合并)。问题是,它没有按预期工作,请参阅更新的QFixed issue,created pull request:您问题中的子对象定义没有引用父对象;您是否尝试过
。抱歉,这是Q中的键入错误。已更正。我已尝试在子字典中使用指定键类型
和值类型
相同的结果我尝试在子字典中指定键类型
和值类型
,结果相同