解析 Unity (Prism) 容器中的数组类型
是否可以在 Unity 容器中注册和解析数组类型?我想做这样的事情:
this.mContainer
.RegisterType<ISomeType, SomeType>()
.RegisterType<ISomeType[], SomeType[]>();
ISomeType[] lSomeTypes = this.mContainer.Resolve<ISomeType[6]>();
如果我不必注册数组类型,并让 Unity 根据 RegisterType
和 单独解析
。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您为特定类型注册多个类型(使用命名注册),那么当容器看到对该类型的数组的依赖项时,它将自动注入所有命名注册。
因此,这将起作用:
每当存在 ISomeType[] 的依赖项(构造函数参数、注入属性等)时,此逻辑就会启动。
请注意,数组注入只会注入命名注册。默认的未命名注册不包含在数组中。
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 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.