按实现类型查找Ninject绑定

Ant*_*ton 5 .net ninject

如何获取绑定到特定实现类型的绑定列表?

IKernel.Bind<IService>().To(implementationType);
Run Code Online (Sandbox Code Playgroud)

这样的事情?

var bindings = IKernel.GetBindings(typeof(IService))
                  .Where(b=>b.ImplementationType==implementationType)
Run Code Online (Sandbox Code Playgroud)

Vad*_*dim 7

不容易.如果你能以某种方式构建一个Ninject Context,你就可以做到

Kernel.GetBindings(typeof(IService))
     .Where(b => b.GetProvider(context).Type == implementationType)
Run Code Online (Sandbox Code Playgroud)

UPDATE

实际上还有另一种方法可以做到这一点.声明绑定时,您可以提供元数据

Kernel.Bind<IService>().To(implementationType)
     .WithMetadata("type", implementationType);
Run Code Online (Sandbox Code Playgroud)

然后你可以通过这样做获得所有绑定

Kernel.GetBindings(typeof(IService))
     .Where(b => b.Metadata.Get<Type>("type") == implementationType)
Run Code Online (Sandbox Code Playgroud)