【问题标题】:Generic Dictionary containing Generic List values in Spring.NetSpring.Net 中包含通用列表值的通用字典
【发布时间】:2011-11-06 23:53:00
【问题描述】:

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

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 类型的属性失败之后,出现了这个令人费解的混乱,所以如果有办法做到这一点,那将以更简洁的方式解决我的问题。

【问题讨论】:

    标签: c#-4.0 spring.net


    【解决方案1】:

    spring 配置的解决方案:

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

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

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 已经看到了,并花了几个小时试图让它适应我的情况。你能说得更具体点吗?
      猜你喜欢
      • 2011-08-03
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多