1
我有以下定義。爲什麼ninject注入一個null可以注入一個空的enumerable?
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說,「找不到匹配的綁定可用」。再次,我期待它發送一個空的枚舉。
那麼,這種行爲真的是設計預期還是有錯誤呢?如果是設計,是否有任何解決方法?
沒有像直接從作者那裏得到答案。然後我會等待代碼更改。或者我會嘗試自己實現它。 :) – jpbochi 2010-11-17 02:47:52
已添加到最新版本。將成爲上升的RC2的一部分。 – 2010-12-01 10:15:12