Generic Dictionary containing Generic List values in Spring.Net

5.2k Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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!

1
On