解析Unity(Prism)容器中的数组类型

M. *_*ley 5 arrays prism unity-container

是否可以在Unity容器中注册和解析数组类型?我想做这样的事情:

this.mContainer
    .RegisterType<ISomeType, SomeType>()
    .RegisterType<ISomeType[], SomeType[]>();
ISomeType[] lSomeTypes = this.mContainer.Resolve<ISomeType[6]>();
Run Code Online (Sandbox Code Playgroud)

如果我不必注册数组类型,并让Unity根据RegisterType<ISomeType, SomeType>()Resolve<ISomeType[]>()单独计算出数组,那就更好了.

Chr*_*res 7

如果为特定类型注册多个类型(使用命名注册),那么当容器看到对该类型数组的依赖时,它将自动注入所有命名注册.

所以这将有效:

this.mContainer
  .RegisterType<ISomeType, SomeImpl1>("one")
  .RegisterType<ISomeType, SomeOtherImpl>("other")
  .RegisterType,ISomeType, AnotherImpl>("another");

ISomeType[] someTypes = mContainer.Resolve<ISomeType[]>();
Run Code Online (Sandbox Code Playgroud)

只要存在ISomeType [] - 构造函数参数,注入属性等的依赖关系,该逻辑就会启动.

请注意,数组注入只会注入命名注册.默认的未命名注册不包含在数组中.