在Spring.NET中将类的类型作为值传递

时间:2010-01-11 14:19:16

标签: .net spring.net

有没有办法将类的类型作为Spring.NET中的值传递?

我的词典看起来像这样:

private Dictionary<int, Type> ContentItemTypes { get; set; }

在代码中我将初始化为:

registry.addContentItemType(0, typeof(ContentItem));

你如何在Spring.NET中做到这一点。到目前为止,我有这个:

<object id="ContentItemTypeRegistry"
      type="Edu3.DomainModel.Framework.Model.Registry.ContentItemTypeRegistry, Edu3.DomainModel.Framework"
      factory-method="GetInstance">
  <property name="ContentItemTypes">
    <dictionary key-type="int" value-type="System.Type">
      <entry key="0">
        <value type="System.Type">Edu3.DomainModel.Framework.Model.ContentItem</value>
      </entry>
    </dictionary>
  </property>
</object>

这是对的吗?

2 个答案:

答案 0 :(得分:5)

甚至更短。

<object id="ContentItemTypeRegistry"
 type="Edu3.DomainModel.Framework.Model.Registry.ContentItemTypeRegistry, Edu3.DomainModel.Framework"
 factory-method="GetInstance">
    <property name="ContentItemTypes">
      <dictionary key-type="int" value-type="System.Type">
        <entry key="0" expression="T(Edu3.DomainModel.Framework.Model.ContentItem)" ></entry>
      </dictionary>
    </property>
  </object>

答案 1 :(得分:1)

也许:

  <object id="ContentItemTypeRegistry"
      type="Edu3.DomainModel.Framework.Model.Registry.ContentItemTypeRegistry, Edu3.DomainModel.Framework"
      factory-method="GetInstance">
    <property name="ContentItemTypes">
      <dictionary key-type="int" value-type="System.Type">
        <entry key="0" value-ref="ContentItemType" ></entry>
      </dictionary>
    </property>
  </object>

  <object id="ContentItemType" type="System.Type, System" factory-method="GetType" singleton="false">
    <constructor-arg value="Edu3.DomainModel.Framework.Model.ContentItem" />
  </object>