1

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 ?

0

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the only place you will simplified syntax for constructor.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.