Unity 中的已解决参数。有人可以解释一下何时使用它吗?
我对 Unity 有点陌生,一切似乎都很好,但我有点迷失何时使用 Unity 中的已解决参数。
谷歌搜索并查看了 MSDN,但仍然不明白何时使用它。
你有一个简单的例子可以说明它的用途吗?
非常感谢您的帮助
I am sort of new to Unity all seems to be fine but I am kind of lost when to use
ResolvedParameter in Unity.
Googled and looked on MSDN but still cannot understand when to use it.
Do you have a simple example that could illustrate it's use.
Thanks a lot for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能希望使用已解析服务的构造函数参数和字符串来配置类型。在这种情况下,您将使用 ResolvedParameter。
You may wish to configure a Type with constructor parameters of a resolved service and a string. In this case you would use ResolvedParameter.
用于方法注入;请参阅 MSDN 上的输入配置信息。向下滚动到“动态配置构造函数、属性和方法注入”,请注意 ResolvedParameter 实际上是 InjectionMethod 构造函数的参数。
我从来没有遇到过需要使用它的情况。构造函数注入将解决 95% 的问题,属性注入将解决另外 5% 的问题。 (警告:我只在几个项目中使用过 Unity,所以我并不声称自己是专家。)
It's for method injection; see Entering Configuration Information on MSDN. Scroll down to "Dynamically Configuring Constructor, Property, and Method Injection" and note that the ResolvedParameter is actually a parameter to the InjectionMethod constructor.
I've never encountered a need to use it. Constructor injection will solve 95% of your issues, and property injection will solve the other 5%. (Caveat: I've only used Unity on a couple of projects, so I don't claim to be an expert.)
正如我所见,当您有一个构造函数,其中至少一个参数无法从容器中获取而其余参数可以时,则可以使用它。在这种情况下,您在实际创建该类型的新实例时声明如何解析每个构造函数参数。
在我的示例中,IConnectionManager 实例从容器获取第一个参数(通过 ResolvedParameter),通过 Container.Resolve 获取第二个参数,第三个参数是硬编码整数。
ResolvedParameter 的行为应与直接 Container.Resolve<> 相同。但看起来干净一点。
As I see it its to be used when you have a constructor where at least one parameter can not be obtained from the container while the rest can. In such a situation you declare how to resolve each ctor parameter when actually creating a new instance of that type.
In my example, the IConnectionManager instance obtains the first parameter from the container (via ResolvedParameter), the 2nd one via Container.Resolve<>, and the 3rd one is a hard-coded integer.
ResolvedParameter should behave equal to a direct Container.Resolve<> but looks a tad cleaner.