1

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?

2
  • FYI - XML DI configuration is considered antiquated because it is difficult to configure and provides no compile-time error checking. These days most DI configuration is done in code inside of the application. Many DI containers are now dropping support for XML. Commented Aug 15, 2015 at 21:53
  • It wasn't my decision to use XML Commented Aug 17, 2015 at 7:21

1 Answer 1

1

Look here:

<property name="Animals">
    <array>
        <value value="Cat"/>
        <value value="Dog"/>
        <value value="Mouse"/>
    </array>
</property>
Sign up to request clarification or add additional context in comments.

1 Comment

@user9993 I hope, but I can be wrong. can you change your config from XML to code?

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.