如何在构造函数中统一传递参数来解决依赖性? [英] How resolve a dependency with unity passing arguments in the constructor?

查看:85
本文介绍了如何在构造函数中统一传递参数来解决依赖性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:



使用C#/ Unity进行构造函数注入?

可以通过在服务的构造函数中统一传递参数来解决依赖项吗?

It is possible to resolve a dependency with unity passing arguments in the constructor of the service to resolve ?

如果这是我怎么做?

推荐答案

创建 InjectionConstructor 并将其传递给 RegisterType()

var unityContainer = new UnityContainer();

ISnuh snuh = new Snuh();
InjectionConstructor injectionConstructor = new InjectionConstructor(snuh);

unityContainer.RegisterType<ICalculator, SimpleCalculator>(injectionConstructor);

如果仅在解析类型时必须这样做,那就是使用ResolverOverride []

If you have to do it only when you resolve the type, that's when ResolverOverride[] would be used:

public static T Resolve<T>(this IUnityContainer container, params ResolverOverride[] overrides);

每个 MSDN


使用ParameterOverride可以覆盖指定的构造函数参数
或参数。

Use ParameterOverride to override the specified constructor parameter or parameters.

这篇关于如何在构造函数中统一传递参数来解决依赖性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆