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