Resolving List<>

Mar 30, 2008 at 2:04 PM
Is there a way to get the container to do the following?
UnityContainer result = new UnityContainer();
result.RegisterType( typeof(IEnumerable<>), typeof(List<>) );
result.Configure<InjectedMembers>().ConfigureInjectionFor( typeof(List<>), new InjectionConstructor( new object[0] ) );
List<object> list = result.Resolve<List<object>>();

Right now this throws the following:
Test method Common.Unity.Extensions.PropertyMapper.Testing.PropertyMapperPolicyTests.VerifyBaseClassesProcessed threw exception:  Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "List`1", name = "". See the InnerException for more details. --->  Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation failed on strategy type Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy, index 2 for build key Build Key[System.Collections.Generic.List`1[System.Object], null] --->  System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B).

I have to configure constructor injection for List<object> rather than the open List<> type to get this to work. It would be nice to configure injection for the open type, much like how we can register for it...
Mar 30, 2008 at 9:48 PM
I don't think this will work. If I understand what you're trying to do. There's the registration of IEnumerable<>, but then you never use it so it's hard to tell if you were planning to resolve IEnumerable<> or List<>.

Assuming you're just trying to mislead me with a red herring :-), I don't think that you can register the empty object[] array as being passed to the constructor. The problem is that List<T> doesn't take an IEnumerable<object>, it takes IEnumerable<T>, and you don't know what T is until later, so there's no concrete object you can register in the constructor.

In the case of the empty array, you're better off calling the default constructor anyway, as it'll get you the same thing. However, I suspect that's not what you were after. Could you give some more details on what you're trying to do?
Mar 31, 2008 at 7:15 PM


ctavares wrote:
In the case of the empty array, you're better off calling the default constructor anyway, as it'll get you the same thing. However, I suspect that's not what you were after. Could you give some more details on what you're trying to do?



Yes I do believe this has to do with a combination with me not fully understanding what the registration of open types does, and me Getting Cute (TM). I was thinking that you could register an open (abstract or interfaced) type, and then have it call the constructor of its mapped type whenever it is needed.