What is the equivalent of Container.GetAllInstances<T> in NInject?

拜拜、爱过 提交于 2019-12-10 14:53:54

问题


I'm building a message broker with NInject, and I need to find all instances in the container that implement Consumes, an interface that marks the class as being able to consume a particular message type.

Is this scenario supported?


回答1:


Answer from Nate:

Multi-resolution (via GetAll) is currently not polymorphic. That means that it will only consider bindings from the exact interface you specify. If you do this:

kernel.Bind<IWorker>().To<WorkerA>();
kernel.Bind<IWorker>().To<WorkerB>();
kernel.Bind<IWorker>().To<WorkerC>();

And then:

kernel.GetAll<IWorker>();

It will return 3 items. However, even if IWorkerA, IWorkerB, and IWorkerC implement IWorker, Ninject will not look at bindings from IWorkerA to WorkerA when you ask for IWorker.

See :

http://groups.google.com/group/ninject/browse_thread/thread/7b6afa06099bc97a#




回答2:


If you if you don't have the polymorphic situation as discussed in the thread that is referenced by Romain's answer, then you shouldn't have any issues as long as you are using Ninject 2. Ninject 1.x did not include this sort of support.



来源:https://stackoverflow.com/questions/1394697/what-is-the-equivalent-of-container-getallinstancest-in-ninject

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