Find Ninject Bindings by Implementation Type

How to get a list of bindings bound to a specific type of implementation?

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

something like that?

var bindings = IKernel.GetBindings(typeof(IService))
                  .Where(b=>b.ImplementationType==implementationType)
+5
source share
1 answer

Not easy. If you can somehow create a Ninject context, you can do

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

UPDATE

There is actually an alternative way to do this. When declaring your bindings, you can provide metadata

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

Then you can get all the bindings by doing this

Kernel.GetBindings(typeof(IService))
     .Where(b => b.Metadata.Get<Type>("type") == implementationType)
+7
source

All Articles