How resolve a dependency with unity passing arguments in the constructor? [duplicate]

纵然是瞬间 提交于 2019-12-24 10:59:25

问题


Possible Duplicate:
Can I pass constructor parameters to Unity’s Resolve() method?
Constructor Injection in C#/Unity?

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

If it is how I can do it ?


回答1:


Create an InjectionConstructor and pass that to RegisterType():

var unityContainer = new UnityContainer();

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

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

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

Per MSDN:

Use ParameterOverride to override the specified constructor parameter or parameters.



来源:https://stackoverflow.com/questions/13713868/how-resolve-a-dependency-with-unity-passing-arguments-in-the-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!