Ninject : Resolving an object by type _and_ registration name/identifier

℡╲_俬逩灬. 提交于 2020-01-11 05:05:07

问题


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

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