为什么 ninject 在可以注入空可枚举的地方注入 null?
我有以下定义。
interface IWeapon {}
class Warrior
{
[Inject]
public IEnumerable<IWeapon> Weapons { get; private set; }
}
如果我调用 new StandardKernel().GetAll
现在,如果我调用 new StandardKernel().Get
,返回的战士的 Weapons
属性中有一个 null
。不应该注入一个空的可枚举吗?
一个次要问题。我修改了 Warrior
类如下。
class Warrior
{
public IEnumerable<IWeapon> Weapons { get; private set; }
public Warrior(IEnumerable<IWeapon> weapons)
{ this.Weapons = weapons; }
}
这次,如果我调用 new StandardKernel().Get
那么,这种行为真的是设计所预期的还是这里存在错误?如果是设计使然,有什么解决办法吗?
I have the following definitions.
interface IWeapon {}
class Warrior
{
[Inject]
public IEnumerable<IWeapon> Weapons { get; private set; }
}
If I call new StandardKernel().GetAll<IRule>()
it understandably returns an empty enumerable.
Now, if I call new StandardKernel().Get<Engine>()
, the returned warrior has a null
in the Weapons
property. Shouldn't an empty enumerable be injected?
A secondary question. I modified the Warrior
class as following.
class Warrior
{
public IEnumerable<IWeapon> Weapons { get; private set; }
public Warrior(IEnumerable<IWeapon> weapons)
{ this.Weapons = weapons; }
}
This time, if I call new StandardKernel().Get<Engine>()
Ninject will throw an ActivationException saying that "No matching bindings are available". Again, I was expecting it to just send an empty enumerable.
So, is this behavior really expected by design or is there a bug here? If it's by design, is there any workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
今天我和伊恩讨论了这个问题和构造函数得分。我们都同意我们应该改变这一点。请耐心等待,直到我们实施它。
I had a discussion about this and constructor scoring today with Ian. We both agreed that we should change this. Just be a little patient and wait until we have implemented it.