문제

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

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

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

도움이 되었습니까?

해결책

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.

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>

This will only work if FieldNameMap is a readonly property.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top