编程语言
首页 > 编程语言> > c# – 包含Spring.Net中的通用字典的通用字典

c# – 包含Spring.Net中的通用字典的通用字典

作者:互联网

我有一个包含属性的对象:

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

如何使用Spring.Net配置此属性?

解决方法:

好吧,在xml中配置这个并不漂亮,考虑将Spring.Net代码配置切换到configure your spring context in C#.

无论如何,要在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.设置通用集合值在in the Spring.net docs中讨论.

通用字典< string,System.Collections.Generic.List< string>>可以以类似的方式配置,这也在this answer中讨论:

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

这不是很漂亮,但你可以稍微improve the readability of your xml using type aliases.

标签:c,dictionary,spring,spring-net
来源: https://codeday.me/bug/20190530/1183578.html