Mike
Mike

Reputation: 107

Find what class an abstract class is bound to in Ninject

How do I find what class is currently bound to the abstract class with Ninject in the following example:

if(conditional)
  Bind<IProducts>().To<Products>();
else
  Bind<IProducts>().To<SqlProducts>();
Type currentType = 'Type based upon current binding of IProducts'

How can I get the value of currentType.

Upvotes: 2

Views: 692

Answers (2)

Mike
Mike

Reputation: 107

kernel.Get<IProducts>().GetType() 

gave me the correct type at runtime.

Upvotes: 1

Ruben Bartelink
Ruben Bartelink

Reputation: 61795

Kernel.Resolve is the low level entrypoint into the resolution machinery that you seek - it doesnt go as far as instantiating the objects.

I suggest downloading the trunk including tests and you'll get examples that cover your exact scenario. Dont treat this as a throwaway commment - the tests are proper clean xUnit.net tests that are exemplary in terms of being short, focused and having good coverage.

You may also find Kernel.Get<T>().GetType()/Kernel.TryGet<T>().GetType() or Kernel.GetAll<T> to be of use, depending on your exact scenario - if you can expand on same, I can make this answer more specific (however all of these instantiate the object rather than let you query the bindings at a low level.

Upvotes: 2

Related Questions