Generic Dictionary containing Generic List values in Spring.Net

天大地大妈咪最大 提交于 2019-12-11 10:13:47

问题


I have an object that contains a property:

public Dictionary<string, List<Hotel>> CityHotels { get; set; }

How do I use Spring.Net to configure this property?

I tried doing this:

    <property name="CityHotels">
      <dictionary key-type="string" value-type="System.Collections.Generic.List&lt;MyNameSpace.Hotel, MyAssembly>" >
        <entry key="SYD">
            <list element-type="MyNameSpace.Hotel, MyAssembly">
            </list>
        </entry>
      </dictionary>
    </property>

but it was unsuccessful:

Error creating context 'spring.root': Could not load type from string value 'System.Collections.Generic.List`2'.

What am I doing wrong?

This convoluted mess came about after I unsuccessfully tried to use Spring.Net to set a property of type ILookup, so if there is a way to do that, that would solve my problem in a cleaner way.


回答1:


Solution for the spring config:

<object id="HotelFinder" type="MyNameSpace.HotelFinder, MyAssembly">
  <property name="CityHotels">
    <dictionary key-type="string" value-type="System.Collections.Generic.List&lt;MyNameSpace.Hotel>, mscorlib">
      <entry key="London" value-ref="hotelsLondon" />
    </dictionary>
  </property>
</object>

<object id="hotelsLondon" type="System.Collections.Generic.List&lt;MyNameSpace.Hotel>, mscorlib">
  <constructor-arg>
      <list element-type="MyNameSpace.Hotel, MyAssembly">
         <ref object="hotelLonden1"/>
         <ref object="hotelLonden2"/>
         <ref object="hotelLonden3"/>
         <ref object="hotelLonden4"/>
       </list>
   </constructor-arg>
 </object>

 <object id="hotelLonden1" type="MyNameSpace.Hotel, MyAssembly" />
 <object id="hotelLonden2" type="MyNameSpace.Hotel, MyAssembly" />
 <object id="hotelLonden3" type="MyNameSpace.Hotel, MyAssembly" />
 <object id="hotelLonden4" type="MyNameSpace.Hotel, MyAssembly" />

In the value-type of the dictionary you don't need to add MyAssembly but mscorlib for System.Collections.Generic.List.

I hope this will help you!




回答2:


See http://www.springframework.net/doc-latest/reference/html/objects.html#objects-creation-generic-types



来源:https://stackoverflow.com/questions/5140037/generic-dictionary-containing-generic-list-values-in-spring-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!