【问题标题】:Resolving array types in a Unity (Prism) container解析 Unity (Prism) 容器中的数组类型
【发布时间】:2011-02-07 15:41:48
【问题描述】:

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

this.mContainer
    .RegisterType<ISomeType, SomeType>()
    .RegisterType<ISomeType[], SomeType[]>();
ISomeType[] lSomeTypes = this.mContainer.Resolve<ISomeType[6]>();

如果我不必注册数组类型,让 Unity 仅根据 RegisterType&lt;ISomeType, SomeType&gt;()Resolve&lt;ISomeType[]&gt;() 计算数组,那就更好了。

【问题讨论】:

标签: arrays unity-container prism


【解决方案1】:

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

所以这会起作用:

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

ISomeType[] someTypes = mContainer.Resolve<ISomeType[]>();

只要有 ISomeType[] 的依赖项——构造函数参数、注入属性等,就会启动此逻辑。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多