开发者

Resolving array types in a Unity (Prism) container

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜