【发布时间】:2023-12-02 06:27:01
【问题描述】:
我有以下定义。
interface IWeapon {}
class Warrior
{
[Inject]
public IEnumerable<IWeapon> Weapons { get; private set; }
}
如果我调用new StandardKernel().GetAll<IRule>(),它可以理解地返回一个空的枚举。
现在,如果我调用new StandardKernel().Get<Engine>(),返回的战士在Weapons 属性中有一个null。不应该注入一个空的枚举吗?
次要问题。我修改了Warrior 类如下。
class Warrior
{
public IEnumerable<IWeapon> Weapons { get; private set; }
public Warrior(IEnumerable<IWeapon> weapons)
{ this.Weapons = weapons; }
}
这一次,如果我调用new StandardKernel().Get<Engine>() Ninject 将抛出一个 ActivationException 说“没有匹配的绑定可用”。同样,我期待它只是发送一个空的枚举。
那么,这种行为真的是设计所期望的还是这里有错误?如果是设计使然,是否有任何解决方法?
【问题讨论】:
标签: .net collections ienumerable ninject