Register an array of dependencies

Jun 15, 2012 at 10:01 PM
Edited Jun 15, 2012 at 10:47 PM

Is there a way with Unity to be able to pass a variable number of dependencies to the constructor at registration. The code below doesnt work. It expects a constructor signature with the exact amount of ICommand parameters as the elements in my array. Can I not have a constructor that takes an IEnumerable<ICommand>?


var resolvedParams = new List<ResolvedParameter<ICommand>>();
                foreach (var prevCommandName in prevCommandNames)
                    resolvedParams.Add(new ResolvedParameter<ICommand>(prevCommandName));
                Container.RegisterType(typeof(ICommand), commandType, name,
                                           new ContainerControlledLifetimeManager(),
                                           new InjectionMember[] { new InjectionConstructor(resolvedParams.ToArray()) });



Jun 15, 2012 at 11:14 PM

The answer was in using the ResolvedArrayParameter. The code below works:


Container.RegisterType(typeof(ICommand), commandType, name,
                                           new ContainerControlledLifetimeManager(),
                                           new InjectionMember[] { new InjectionConstructor(new ResolvedArrayParameter<ICommand>(resolvedParams.ToArray())) });