Ninject:将某些东西绑定到自身是什么意思? [英] Ninject: What does it mean to bind something to itself?
问题描述
Ninject具有self binding
的功能,例如Bind<Samurai>().ToSelf();
Ninject has the functionality of self binding
like Bind<Samurai>().ToSelf();
我读到了有关此内容的信息,但我没有得到任何重视或如何发挥作用.任何评论表示赞赏.
I read about this but I don't get the importance or how this can be useful. Any comments are appreciated.
推荐答案
如果Ninject找到需要创建的对象,并且它的构造函数带有Samurai
参数,则它不知道如何实例化它.
If Ninject finds a object that needs to be created and it has a constructor that has a Samurai
parameter it does not know how to instantiate it.
但是当您使用Bind<Samurai>().ToSelf();
时,Ninject知道需要创建一个Samurai
才能传递给Samurai
参数.
But when you use Bind<Samurai>().ToSelf();
then Ninject knows that a Samurai
needs to be created to pass to the Samurai
parameter.
如果没有该绑定,则ninject不知道要传递什么,例如,可能存在SamuraiSubClass
类型.但是通过明确地说,当Ninject找到需要为其创建Samurai
的Samurai
参数时,Ninject确切地知道该怎么做.
If that binding was not there then ninject didn't know what to pass, for example there might have been a SamuraiSubClass
type. But by explicitly saying that when Ninject finds a Samurai
parameter that it needs to create a Samurai
for that parameter then Ninject knows exactly what to do.
这篇关于Ninject:将某些东西绑定到自身是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!