Passing list of strings or array of strings to Unity Injection Constructor (Config-Based)

I can't seem to get the unity when I try to pass an array of strings to a list of constructor parameters when using XML config.

When I try the following:

<typeConfig ...>
  <constructor ...>
    <param ... parameterType="System.String[]">
     <array>    
      <value.../>
      <value.../>
     </array>
    </param> 
  </constructor>
</typeConfig>

      

for a c'tor that looks like this:

void Foo(string[] inputParams_){ ... }

      

It always fails in Unity's FindConstructor (...) method, stating that it cannot find the c'tor defining the type of the String.String parameter

Does anyone know how to successfully traverse an array of bites into this type of c'tor? If not, how can I do this with a list of strings if c'tor was to accept an IList?

Thanks!

+2


a source to share


3 answers


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

This will work:



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

      

+2


a source


I usually prefer to configure Unity in code, so I cannot be helpful if config is required. But....

Typically I would use ConstructorInjector when registering:

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



But according to: Can I pass constructor parameters to Unity's Resolve () method?

Unity 2 now also includes the ability to dynamically pass parameters to the constructor at resolution time:

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

+1


a source


You may need to fully qualify the type name:

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

      

If you wish, you can opt out of the version if you don't care.

0


a source







All Articles