Find Ninject Bindings by Implementation Type
How do I get a list o开发者_高级运维f 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)
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)
精彩评论