开发者

Passing List of Strings or Array of strings into Unity Injection Constructor (Config-Based)

开发者 https://www.devze.com 2022-12-28 15:36 出处:网络
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.

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开发者_JAVA百科:

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!


You don't need the attribute 'parameterType' for the element 'param'

This will work:

    <constructor>
      <param name="eventsDefinitions">
        <array>
          <value value="PhaseLoss"/>
          <value value="DCRC" />
          <value value="PhaseRotation" />
        </array>
      </param>
    </constructor>


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:

container.Configure() .ConfigureInjectionFor(new InjectionConstructor([value]))

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:

"container.Resolve(new ParameterOverrides { { "name", "bar" }, { "address", 42 } });"


Maybe you will have to fully qualify the type name:

System.String[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Optionally, you may drop the version if you don't care/know.

0

精彩评论

暂无评论...
验证码 换一张
取 消