0

I am using the Unity Container to inject a class into another class thru the constructor by passing the Interface to the Target classes constructor. I want to pass arguments to a constructor of th injected class. Can anyone tell me how to do this?

For example in the example below I want to pass a 2 parameters to the constructor of adminmanager which is being injected into MyClass.

 public class MyClass
{
    IAdminManager AdminManager;

     public MyClass(IAdminManager adminManager)
    {
        AdminManager = adminManager;
    }

}

1
  • 1
    Can you post the ctor for AdminManager as well. Commented Apr 18, 2012 at 8:16

1 Answer 1

1

You can override constructor parameters when resolving an object via unity:

IAdminManager adminMgr = 
    container.Resolve<IAdminManager>(new ParameterOverride("param", myValue));
MyClass c = 
    container.Resolve<MyClass>(new ParameterOverride("adminManager", adminMgr));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.