简体   繁体   中英

Spring.NET Merge Dictionary Using Key-Ref

Related: Add entry to dictionary in child object

I'm trying to create merged parent/child inheritance objects in my Spring.NET config like so:

<object name=".parent" abstract="true" type="Assembly.KeysAndValues, Assembly">
  <property name="TheDictionary">
    <dictionary key-type="Assembly.KeyType, Assembly" value-type="Assembly.ValueType, Assembly">
      <entry key-ref="Object1" value-ref="Value1" />
      <entry key-ref="Object2" value-ref="Value2" />
      <entry key-ref="Object3" value-ref="Value3" />
    </dictionary>
  </property>
</object>

<object name="child" parent=".parent">
  <property name="TheDictionary">
    <dictionary merge="true">
      <entry key-ref="Object2"><null/></entry>
    </dictionary>
  </property>
</object>

The main difference is that I'm using "key-ref" instead of "key" as in the related question. I would still expect it to work, but it doesn't. When the "child" object is being created, I get an "An item with the same key has already been added." exception. That's the whole point, I want to replace Value2 with null using the merge.

Here is the exception:

InnerException: System.ArgumentException
       HResult=-2147024809
       Message=An item with the same key has already been added.
       Source=mscorlib
       StackTrace:
            at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
            at System.Collections.Generic.Dictionary`2.System.Collections.IDictionary.Add(Object key, Object value)
            at Spring.Objects.Factory.Config.ManagedDictionary.Resolve(String objectName, IObjectDefinition definition, String propertyName, ManagedCollectionElementResolver resolver)
            at Spring.Objects.Factory.Support.ObjectDefinitionValueResolver.ResolvePropertyValue(String name, IObjectDefinition definition, String argumentName, Object argumentValue)
            at Spring.Objects.Factory.Support.ObjectDefinitionValueResolver.ResolveValueIfNecessary(String name, IObjectDefinition definition, String argumentName, Object argumentValue)
            at Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.ApplyPropertyValues(String name, RootObjectDefinition definition, IObjectWrapper wrapper, IPropertyValues properties)
            at Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.PopulateObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper)
            at Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.ConfigureObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper)
            at Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InstantiateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching, Boolean suppressConfigure)

Does the spring collection merging not support key-ref or is something else going on here?

After some debugging against the Spring.NET source, I've created a pull request for the Spring.NET Github project because I believe this is a bug in the library. With my change, the dictionary merging worked. For details on what the problem is, see the pull request:

https://github.com/spring-projects/spring-net/pull/84

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM