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!

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

thanks, but the questions is specifically about configuratin files
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.
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.

1 Comment

hmm, not sure will try it, but system.string doesnt require qualification elsewhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.