Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all concrete types from ninject

Is there a way with ninject to get the concrete types from a binding? I don't want to instantiate the implementations, just get the type.

I want do to do something like this

Type[] concreteTypes = Kernel.GetBindings(typeof(IController)).
    GetImplementingTypes();

My Kernel is just a StandardKernel.

I want to eventually reflect against those types.

like image 626
Daniel A. White Avatar asked Oct 09 '22 15:10

Daniel A. White


1 Answers

The only way is to get all instances using kernel.GetAll<IController>(). Ninject does not have a set binding mapping. The implementation is determined when the instances are resolved through injection or get calls. This is because Ninject allows for conditional and implicit bindings.

If you set up an MVC3 application, you do not actually register your controllers as Ninject will determine which controller to load and what to inject into it. It does this by lazily creating an implicit binding.

The closest you can come is to extend StandardKernel and access the protected binding set and find all bindings that are not conditional. Once you have all non-conditional bindings, you may be able to pull apart the binding, but I can't remember exactly as they may be hidden behind the StandardProvider class.

What are you doing that you need to know the exact binding configuration for a particular interface?

like image 107
Ian Davis Avatar answered Oct 13 '22 12:10

Ian Davis