Search code examples
c#xmlinversion-of-controlcastle-windsor

How to add string array component to XML configuration?


I have a number of components in XML configuration file describing different configurations. Each configuration has a string array property IgnoredCountries. Now I have to make all the countries ignored for some configurations. In order to reduce configuration file size (and make it more comprehensive) I would like to create separate component allCountries that will contain all the country codes and just reference it where needed. The problem is to properly define component for string array.

I'm using Castle Windsor as a IoC-container. I've tried to declare the component using the following code, but unsuccessfully.

<component id="allCountries" service="System.String[], mscorlib">
  <parameters>
    <collection>
      <list>
        <item>AF</item>
        <item>AL</item>
        <item>AS</item>
        <!-- there's a lot of items -->
      </list>
    </collection>
  </parameters>
</component>

When trying to declare the component like shown above, I receive an exception saying that the component allCountries is waiting for the following dependencies: Parameter '' which was not provided. Did you forget to set the dependency?


Solution

  • That doesn't work because string or string[] is not a valid component type. Components are meant to be classes, not primitives.

    Ideally, you wouldn't use XML for that, but if you're sure it makes sense in your case you might put it in an xml property instead. I'm not 100% sure it would work but give it a try. See the example in documentation here.

    A cleaner solution would be to wrap it in a class, rather than relying on string[]