包含Spring.Net中的通用列表值的通用字典

时间:2011-02-28 09:16:41

标签: c#-4.0 spring.net

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

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

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

我试过这样做:

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

但是没有成功:

  

创建上下文'spring.root'时出错:无法从字符串值'System.Collections.Generic.List`2'加载类型。

我做错了什么?

在我尝试使用Spring.Net设置类型ILookup的属性失败后,出现了这个令人费解的混乱,所以如果有办法做到这一点,那将以更清洁的方式解决我的问题。 / p>

2 个答案:

答案 0 :(得分:5)

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

在字典的value-type中,您不需要为System.Collections.Generic.List添加MyAssembly而是mscorlib。

我希望这会对你有所帮助!

答案 1 :(得分:0)

相关问题