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

发布于 2024-10-19 16:11:11 字数 909 浏览 7 评论 0原文

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

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

How do I use Spring.Net to configure this property?

我尝试这样做:

    <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,所以如果有办法做到这一点,那就可以以更干净的方式解决我的问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡忘如思 2024-10-26 16:11:11

spring配置的解决方案:

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

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

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

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文