Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Ninject Bindings by Implementation Type

Tags:

.net

ninject

How do I get a list of bindings which are bound to a particular implementation type?

IKernel.Bind<IService>().To(implementationType);

something like this ?

var bindings = IKernel.GetBindings(typeof(IService))
                  .Where(b=>b.ImplementationType==implementationType)
like image 223
Anthony Johnston Avatar asked Mar 30 '11 14:03

Anthony Johnston


1 Answers

Not easily. If you can somehow construct a Ninject Context, you can do

Kernel.GetBindings(typeof(IService))
     .Where(b => b.GetProvider(context).Type == implementationType)

UPDATE

Actually there is an alternate way to do it. When declaring your bindings you can supply metadata

Kernel.Bind<IService>().To(implementationType)
     .WithMetadata("type", implementationType);

Then you can get all bindings by doing this

Kernel.GetBindings(typeof(IService))
     .Where(b => b.Metadata.Get<Type>("type") == implementationType)
like image 190
Vadim Avatar answered Oct 03 '22 18:10

Vadim