user9993
user9993

Reputation: 6170

Initializing a list within Unity Configuration XML

I need to initialize an object which has a number of properties. These properties are IList<string>. I want to add strings to these via the configuration file. I'm using Unity configuration XML but I am unsure of the XML syntax to describe what I am trying to achieve.

This is the class of properties:

public class MyClass : IMyClass
{
    public IList<string> Animals { get; set; }
    public IList<string> People { get; set; }
    public IList<string> Objects { get; set; }
}

And this is my XML so far (using a non-real world for simplicity):

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <container>
    <register>
      <register type="IMyClass" mapTo="MyClass">
        <property name="Animals">
           Somehow use <method> to `Insert` these strings to the properties IList
           Cat
           Dog
           Mouse
        </property>
        <property name="People">
           Bob
           Joe
           Jack
        </property>
        <property name="Objects">
           Chair
           Door
        </property>
      </register>
    </register>
  </container>
</unity>

According to MSDN to call a method you use this XML:

<register type="MyLogger">
    <method name="Initialize">
        <param name="loggerSettings" />
    </method>
</register>

But I do not know how to mix the method call in with the <property> tag.

How should the XML be written to allow for Unity to call methods on the properties to add?

Upvotes: 1

Views: 1366

Answers (1)

Backs
Backs

Reputation: 24903

Look here:

<property name="Animals">
    <array>
        <value value="Cat"/>
        <value value="Dog"/>
        <value value="Mouse"/>
    </array>
</property>

Upvotes: 1

Related Questions