在Spring.Net中注入Hashtable

时间:2011-11-10 14:33:04

标签: c# dependency-injection inversion-of-control spring.net

你能帮我配置一下Spring.Net,在对象属性中注入一个预先定义的哈希表吗?

我做过这样的事情:

<object name="myHashtable" type="Hashtable">
  <map>
    <entry key="key1" value="value1" />
    <entry key="key2" value="value2" />
  </map>
</object>

但似乎MAP节点不存在!

解决

确切的配置是:

<object id="myHashtable" type="System.Collections.Hashtable">
    <constructor-arg>
        <dictionary key-type="decimal?" value-type="int" merge="0">
            <entry key="1" value="31" />
            <entry key="2" value="32" />
            <entry key="3" value="33" />
            <entry key="4" value="34" />
        </dictionary>
    </constructor-arg>
</object>

1 个答案:

答案 0 :(得分:2)

使用IDictionary作为构造函数创建一个哈希表,例如:

<object id="MyObject" type="MyClass">

  <property name="MyHashTable">
    <object type="System.Collections.Hashtable, mscorlib">
      <constructor-arg name="d">
        <dictionary>
          <entry key="key1" value="value1" />
          <entry key="key2" value="value2" />
        </dictionary>
      </constructor-arg>
    </object>
  </property>

</object>