Unity - 按名称和条件解决所有问题
我想知道是否可以通过对它们注册的名称的某些条件来解决 Unity 中的所有依赖关系。
例如: 解析所有注册名称以“ProcessA”开头的接口。
如果没有办法做到这一点,那么也许我如何扩展 Unity 来允许这样做。
I was wondering if it is possible to resolve all dependancies in Unity by some condition on the name that they were registered.
For example:
Resloving all interfaces registered where the name registered starts with "ProcessA".
And if there is no way to do this then perhaps how can i extend Unity to allow this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够使用
Registrations
来执行此操作,我会推荐一种扩展方法,而不是直接扩展 Unity:使用此扩展方法:
You should be able to use
Registrations
to do this and I would recommend an extension method rather than extending Unity directly:Using this extension method:
只是添加到迈克尔的答案,我扩展了它,允许您使用相同的名称注册,但使用解析所有来仅解析针对该名称注册的那些。
允许像这样的注册和解析:
scope1Foos 将包含 Foo1 和 Foo2,scope2Foos 将包含 Foo3 和 Foo4
Just adding to Michael's answer, I extended that to allow you to register with the same names yet use resolve all to only resolve those registered against that name.
Allows for registration and resolution like this:
scope1Foos will container both Foo1 and Foo2, scope2Foos will contain both Foo3 and Foo4