Property Inject an Array with Spring.Net

不羁的心 提交于 2019-12-22 08:23:40

问题


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[].

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

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


回答1:


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>

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>


来源:https://stackoverflow.com/questions/1175516/property-inject-an-array-with-spring-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!