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

问题描述

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

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);

每个:

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

09-25 18:54