将字符串列表或字符串数组传递到 Unity 注入构造函数(基于配置)
在使用 XML 配置时尝试将字符串数组传递到构造函数参数列表中时,我似乎无法使统一工作。
当我尝试以下操作时:
<typeConfig ...>
<constructor ...>
<param ... parameterType="System.String[]">
<array>
<value.../>
<value.../>
</array>
</param>
</constructor>
</typeConfig>
对于看起来像这样的 c'tor:
void Foo(string[] inputParams_){ ... }
它总是在 Unity 的 FindConstructor(...) 方法中失败,指出它无法找到 c'tor mathcing the 参数类型 String.String
有谁知道如何成功地将一系列刺痛传递给这种类型的 c'tor?如果没有,如果 c'tor 接受 IList,我该如何处理字符串列表?
谢谢!
I cannot seem to get unity working when attempting to pass in an array of strings into a constructor parameter list, while using XML configuration.
When I try the following:
<typeConfig ...>
<constructor ...>
<param ... parameterType="System.String[]">
<array>
<value.../>
<value.../>
</array>
</param>
</constructor>
</typeConfig>
for a c'tor which looks like this:
void Foo(string[] inputParams_){ ... }
It always fails in Unity's FindConstructor(...) method stating that it cannot find a c'tor mathcing the parameter type of String.String
Does anyone know how to pass an array of stings successfully into this type of c'tor? If not, how can I do so with a list of strings, if the c'tor were to accept an IList?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要元素“param”的属性“parameterType”
这将起作用:
You don't need the attribute 'parameterType' for the element 'param'
This will work:
通常我更喜欢在代码中配置 Unity,因此如果必须进行配置,我可能不会那么有帮助。但是......
通常我会使用
注册时的ConstructorInjector:
但根据:
我可以将构造函数参数传递给 Unity 的 Resolve() 方法吗?
Unity 2 现在还应该包含在解析期间将参数动态传递到构造函数的功能:
Typically I prefer to configure Unity in code, so I may not be that helpful if config is a must. But ....
Typically I'd use a
ConstructorInjector during registration:
But according to:
Can I pass constructor parameters to Unity's Resolve() method?
Unity 2 should now also includes the ability to pass parameters into the constructor dynamically during resolution:
也许您必须完全限定类型名称:
或者,如果您不关心/不知道,您可以删除该版本。
Maybe you will have to fully qualify the type name:
Optionally, you may drop the version if you don't care/know.