4

プロパティを含むオブジェクトがあります:

public Dictionary<string, Dictionary<string, List<ContextMenuItemModel>>> ContextMenuModel { get; set; }

Spring.Netを使用してこのプロパティを構成するにはどうすればよいですか?

4

1 に答える 1

6

さて、これをxmlで構成するのはきれいではありません。Spring.Netコード構成に切り替えて、C#でSpringコンテキストを構成することを検討してください。

とにかく、xmlでこれを行うには、汎用の.netコレクションのコンストラクターを使用します。たとえばList<T>、コンストラクターを取るIList<T>ので、次のように文字列のリストを構成できます。

<object id="list1" type="System.Collections.Generic.List&lt;string>">
  <constructor-arg>
    <list element-type="string">
      <value>abc</value> 
      <value>def</value> 
    </list>
  </constructor-arg>
</object>

使用は合法的なxmlではない&lt;ため、xmlではを使用する必要があることに注意してください。<ジェネリックコレクション値の設定については、Spring.netのドキュメントで説明されています。

ジェネリックDictionary<string, System.Collections.Generic.List<string>>は同様の方法で構成できますが、これについてもこの回答で説明しています。

<object id="dic1" type="System.Collections.Generic.Dictionary&lt;string, System.Collections.Generic.List&lt;string>>">
  <constructor-arg>
    <dictionary key-type="string" value-type="System.Collections.Generic.List&lt;string>">
      <entry key="keyToList1" value-ref="list1" />
      <entry key="keyToList2" value-ref="list2" /> 
    </dictionary>
  </constructor-arg>
</object>

そして、あなたはおそらく次のものが今来るのを見るでしょう:

<object id="dic0" type="System.Collections.Generic.Dictionary&lt;string, System.Collections.Generic.Dictionary&lt;string, System.Collections.Generic.List&lt;string>>>">
  <constructor-arg>
    <dictionary key-type="string" value-type="System.Collections.Generic.Dictionary&lt;string, System.Collections.Generic.List&lt;string>>">
      <entry key="keyToDic1 " value-ref="dic1" />
    </dictionary>
  </constructor-arg>  
</object>

注入できるもの:

<object id="MyObject" type="MyNamespace.MyClass, MyAssembly">
  <property name="ContextMenuModel" ref="dic0" />
</object>

これは実際にはきれいではありませんが、型エイリアスを使用してxmlの可読性をわずかに向上させることができます。

于 2012-04-22T08:37:12.087 に答える