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

Sco*_*son 1 spring.net c#-4.0

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

public Dictionary<string, List<Hotel>> CityHotels { get; set; }
Run Code Online (Sandbox Code Playgroud)

如何使用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>
Run Code Online (Sandbox Code Playgroud)

但它没有成功:

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

我究竟做错了什么?

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

Ger*_*ard 5

弹簧配置的解决方案:

<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" />
Run Code Online (Sandbox Code Playgroud)

在字典的值类型中,您不需要添加MyAssembly,而是添加mscorlib System.Collections.Generic.List.

我希望这能帮到您!