2

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!

miguel
  • 2,961
  • 4
  • 26
  • 34

3 Answers3

2

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>
javi
  • 74
  • 1
  • 7
1

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 } });"

Community
  • 1
  • 1
Doobi
  • 4,844
  • 1
  • 22
  • 17
  • thanks, but the questions is *specifically* about configuratin files – miguel May 01 '10 at 15:16
  • No worries. Another worthless try is - I've seen the notation "'1" dotted around text configuration, something like "String'1" usually when referring to some sort of generic collection. – Doobi May 02 '10 at 14:35
0

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.

Peter Lillevold
  • 33,668
  • 7
  • 97
  • 131