属性使用Spring.Net注入数组

编程入门 行业动态 更新时间:2024-10-28 20:23:58
本文介绍了属性使用Spring.Net注入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在使用Spring.Net IoC容器,可以使用它来注入类型为IList甚至IList<T>的属性,但是我对如何注入类型的属性有些困惑string[].

I've been using the Spring.Net IoC container and can use it to inject properties that are of type IList and even IList<T> but I'm a bit stumped as to how to inject a property thats of type string[].

在XSD中似乎没有定义<array>元素,并且使用<list> <value> </list>也不起作用.

There doesn't seem to be an <array> element defined in the XSD's and using <list> <value> </list> doesn't work either.

如果任何人都可以发布xml,我需要使用数组作为属性注入,将不胜感激

If anyone could post the xml I need to inject using an array for a property it'd be much appreciated

推荐答案

如所述在此处的文档中,您可以将字符串数组作为逗号分隔的字符串注入(不确定是否需要使用语法将实际的逗号转义为字符串).换句话说,您的配置看起来像这样:

As mentioned here in the documentation you can inject a string array as a comma delimited string (not sure what the syntax is for escaping actual commas in strings if necessary). In other words your config would look something like this:

<object id="MyObject" type="Blah.SomeClass, Blah" > <property name="StringArrayProperty" value="abc,def,ghi" /> </object>

如果您需要更复杂的内容(例如,如果要从其他参考中查找各个值而不是对其进行硬编码),则可以使用以下语法手动构造string[]:

Manually constructing a string[] with the following syntax also works, if you need something more complex (for example if you're looking the individual values up from some other reference rather than hard coding them):

<object id="TestStrArr" type="string[]" > <constructor-arg value="3" /> <property name="[0]" value="qwe" /> <property name="[1]" value="asd" /> <property name="[2]" value="zxc" /> </object> <object id="MyObject" type="Blah.SomeClass, Blah" > <property name="StringArrayProperty" ref="TestStrArr" /> </object>

更多推荐

属性使用Spring.Net注入数组

本文发布于:2023-06-04 22:09:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/506063.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   属性   Spring   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!