本文介绍了禁用Ninject 2+中非显式绑定类的隐式绑定/注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果您从NInject请求一个未绑定的对象,则默认行为是(如果有合适的构造函数可用)似乎是为该合适的对象创建实例.
If you request an unbound object from NInject, then the default behaviour is (if a suitable constructor is available) appears to be to create an instance of the appropriate object.
我想禁用此行为(我很难调试问题,因为某些东西是自动绑定的,而不是在模块中获取我的自定义绑定). 这个问题暗示可能,但是我无法从NInject Wiki找到答案.
I'd like to disable this behaviour (I had a difficult to debug issue because something was auto-bound instead of picking up my custom binding in a module). This question hints that it is possible, but I'm unable to find the answer from the NInject wiki.
推荐答案
创建后从内核组件中删除SelfBindingResolver
:
Remove the SelfBindingResolver
from the kernel components after creation:
kernel.Components.RemoveAll<IMissingBindingResolver>();
kernel.Components.Add<IMissingBindingResolver, DefaultValueBindingResolver>();
这篇关于禁用Ninject 2+中非显式绑定类的隐式绑定/注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!