Is it possible to register and resolve array types in a Unity container? I'd like to do something like this:
this.mContainer
.RegisterType<ISomeType, SomeType>()
.RegisterType<ISomeType[], SomeType[]>();
ISomeType[] lSomeTypes = this.mContainer.Resolve<ISomeType[6]>();
It would be even better if I didn't have to register the array type, and have Unity figure out the array based on RegisterType<ISomeType, SomeType>()
and Resolve<ISomeType[]>()
alone.
If you register multiple types for a particular type (using named registrations), then when the container sees a dependency on an array of that type, it'll automatically inject all the named registrations.
So this will work:
this.mContainer
.RegisterType<ISomeType, SomeImpl1>("one")
.RegisterType<ISomeType, SomeOtherImpl>("other")
.RegisterType,ISomeType, AnotherImpl>("another");
ISomeType[] someTypes = mContainer.Resolve<ISomeType[]>();
This logic will kick in whenever there's a dependency of ISomeType[] - constructor parameter, injected property, etc.
Note that the array injection will only inject named registrations. The default, unnamed registration is not included in the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With