gpt4 book ai didi

c# - Ninject multi-injection 并没有我想象的那么贪心!怎么会?

转载 作者:可可西里 更新时间:2023-11-01 08:43:08 26 4
gpt4 key购买 nike

如果我有一个类,其中的 ctor 设置为像这样的多重注入(inject):

public Shogun(IEnumerable<IWeapon> allWeapons)
{
this.allWeapons = allWeapons;
}

绑定(bind)设置如下:

Bind<IWeapon>().To<Sword>();
Bind<IWeapon>().To<Dagger>().WhenInjectedInto<Shogun>();

那么我希望 build 幕府将军时两种武器都已注入(inject)?但事实并非如此 - 它只会获得 Dagger 。

如果我像这样添加进一步的绑定(bind):

Bind<IWeapon>().To<Sword>();
Bind<IWeapon>().To<Dagger>().WhenInjectedInto<Shogun>();
Bind<IWeapon>().To<Shuriken>().WhenInjectedInto<Shogun>();

然后幕府将军得到了 Dagger 和手里剑。 WhenInjectedInto<T>()看起来它应该只约束它所应用的绑定(bind)而不影响其他绑定(bind)。我发现这种行为非常具有误导性。

谁能解释一下这里发生了什么?

最佳答案

关于c# - Ninject multi-injection 并没有我想象的那么贪心!怎么会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7297342/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com