Jam*_*rld 11 c# dependency-injection ninject inversion-of-control constructor-injection
如果我有一个ctor设置为多次注入的类,如下所示:
public Shogun(IEnumerable<IWeapon> allWeapons)
{
this.allWeapons = allWeapons;
}
Run Code Online (Sandbox Code Playgroud)
绑定设置如下:
Bind<IWeapon>().To<Sword>();
Bind<IWeapon>().To<Dagger>().WhenInjectedInto<Shogun>();
Run Code Online (Sandbox Code Playgroud)
然后我会期望将Shogun用两种武器注入?但事实并非如此 - 它只会得到匕首.
如果我添加这样的进一步绑定:
Bind<IWeapon>().To<Sword>();
Bind<IWeapon>().To<Dagger>().WhenInjectedInto<Shogun>();
Bind<IWeapon>().To<Shuriken>().WhenInjectedInto<Shogun>();
Run Code Online (Sandbox Code Playgroud)
然后Shogun得到了Dagger和Shuriken.WhenInjectedInto<T>()看起来它应该只限制它应用的绑定而不影响其他绑定.我发现这种行为非常误导.
有人能解释一下这里发生了什么吗?