Unity will understand that T[]
(array) dependencies are a list of things (but not IEnumerable<T>
, nor List<T>
). When you change the constructor of ClassToResolve
to take an CollectionItem[]
instead of a List<CollectionItem>
you can configure your CollectionItem
types as follows:
container.RegisterType<CollectionItem, CollectionItemA>("a");
container.RegisterType<CollectionItem, CollectionItemB>("b");
container.RegisterType<CollectionItem, CollectionItemC>("c");
container.RegisterType<CollectionItem, CollectionItemD>("d");
The trick here is to name all the registrations. A default (nameless) registration will never be part of a sequence dependency in Unity.
Now you can resolve ClassToResolve
without the need to register it:
container.Resolve<ClassToResolve>();
If you rather inject List<CollectionItem>
you can add the following registration:
container.RegisterType<IList<CollectionItem>, CollectionItem[]>();
And for IEnumerable<CollectionItem>
you can add the following line:
container
.RegisterType<IEnumerable<CollectionItem>, CollectionItem[]>();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…