Matt Abbott
Matt Abbott

Reputation: 11

Unity Dependancy Injection passing runtime parameter values to a Constructor?

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

}

Upvotes: 0

Views: 1396

Answers (1)

PVitt
PVitt

Reputation: 11760

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

Upvotes: 1

Related Questions