使用Key-Ref的Spring.NET合并字典

时间:2014-09-16 20:33:42

标签: c# spring merge spring.net

相关:Add entry to dictionary in child object

我正在尝试在Spring.NET配置中创建合并的父/子继承对象,如下所示:

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

主要区别在于我在相关问题中使用“key-ref”而不是“key”。我仍然希望它可以工作,但事实并非如此。当创建“child”对象时,我得到一个“已经添加了具有相同键的项目”。例外。这就是重点,我想使用合并将null替换为null。

以下是例外:

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)

Spring合并是否不支持key-ref或者其他什么东西在这里?

1 个答案:

答案 0 :(得分:0)

在针对Spring.NET源进行一些调试之后,我已经为Spring.NET Github项目创建了一个pull请求,因为我相信这是库中的一个错误。随着我的改变,字典合并工作。有关问题的详细信息,请参阅拉取请求:

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