问题
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<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<MyNameSpace.Hotel>, mscorlib">
<entry key="London" value-ref="hotelsLondon" />
</dictionary>
</property>
</object>
<object id="hotelsLondon" type="System.Collections.Generic.List<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