2

I would like to know how can we inject List of all child objects of an abstract class to a constructor using xml config.

E.g.:

Class Test
{
 public Test(List<A> instances) // So this list should have instance of B & C
{

}
}

abstract Class A
{

}

Class B: A
{

}

Class C: A
{

}

Thanks!!!

Deepak
  • 303
  • 3
  • 14

1 Answers1

1

You can use following config if you change a type of a Test's constructor argument on IList<A>:

<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <namespace name="System.Collections.Generic" />
    <namespace name="ConsoleApplication1" />
    <assembly name="ConsoleApplication1" />
    <container>
      <register type="A" mapTo="B" name="B" />
      <register type="A" mapTo="C" name="C" />
      <register type="IList`1[[A]]" mapTo="A[]" />
    </container>
  </unity>
</configuration>

So the trick is to map IList<A> interface on A[] - see this question for details.

Community
  • 1
  • 1
stop-cran
  • 4,229
  • 2
  • 30
  • 47