问题
I am looking for a way to do something like this with Ninject :
// Sample from the Unity application block
IMyService result = myContainer.Resolve<IMyService>("Data");
( from http://msdn.microsoft.com/en-us/library/cc440957.aspx )
Is it possible?
回答1:
Ninject 2.0 has this capability:
Bind<IMyService>().To<MyServiceA>().Named("Data");
Bind<IMyService>().To<MyServiceB>().Named("SomethingElse");
kernel.Get<IMyService>("Data"); // will return MyServiceA
回答2:
AFAIK there is no way to do that directly in Ninject, but you can use Contextual Binding instead.
来源:https://stackoverflow.com/questions/533260/ninject-resolving-an-object-by-type-and-registration-name-identifier