Constructor injection with initialized generic list

Jul 2, 2010 at 8:54 AM

 

I want to resolve an instance of class Foo with a generic list  filled with FooBar objects.  Here is an example.

I ve  expected, that the constructor of Foo is called with a generic List of  three FooBars.

main()

{

Foo foo = aUnityContainer.resolve(typeof(Foo),  "FooWithInitailizedList");

}

 

public class Foo{

    public Foo(List<FooBar> list_in);

}

class FooBar

{

public Foo();

}

 

 

     <register type="Foo" name="FooWithInitailizedList" >
        <constructor>
          <param name="list_in">
            <dependency name="ListOfDefaultFooBars" />
          </param>
        </constructor>
      </register>

      <register type="System.Collections.Generic.List`1[[Testclasses.FooBar, Testclasses]]" name="ListOfDefaultFooBars" >
        <constructor>
          <param name="collection">
            <array>
              <dependency name="FooBar" />
              <dependency name="FooBar" />
              <dependency name="FooBar" />
            </array>
          </param>
        </constructor>
      </register>

      <register type="FooBar" name="FooBar" >
        <constructor>
        </constructor>
      </register>

 

 How is it possible to resolve an instance of class Foo with a generic list  filled with FooBar objects?

Bernhard

Jul 2, 2010 at 9:19 PM

It's not supported out of the box. You could register an instance or a factory using the API. Otherwise, you'd need to write a custom config extension to do it.