我有以下定义。
interface IWeapon {}
class Warrior
{
[Inject]
public IEnumerable<IWeapon> Weapons { get; private set; }
}
如果我打电话
new StandardKernel().GetAll<IRule>()
可以理解,它返回一个空的可枚举项。
现在,如果我打电话
new StandardKernel().Get<Engine>()
,返回的战士有一个
null
在
Weapons
财产。不应该注入空的可枚举项吗?
第二个问题。我修改了
Warrior
分类如下。
class Warrior
{
public IEnumerable<IWeapon> Weapons { get; private set; }
public Warrior(IEnumerable<IWeapon> weapons)
{ this.Weapons = weapons; }
}
这次,如果我打电话
new StandardKernel().Get<引擎>()
Ninject将抛出一个ActivationException,表示“没有可用的匹配绑定”。再一次,我希望它只是发送一个空的枚举。
那么,这种行为真的是设计所期望的吗?还是这里有bug?如果是设计的,有什么解决办法吗?