mag*_*gol 4 c# dependency-injection ninject ninject-2
我正在使用MVVM灯并设置绑定如下:
class TestModule:NinjectModule
{
public override void Load()
{
Bind<ICollection<Element>>().To<Collection<Element>>();
Bind<Element>().ToSelf();
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试获取ICollection时,我得到一个包含ONE元素的集合.我期待一个很好的收藏.
var _kernel = new StandardKernel(new TestModule());
var col = _kernel.Get<ICollection<Element>>();
Console.WriteLine("Count={0}", col.Count); //Write "Count=1", Expect "Count=0"
Run Code Online (Sandbox Code Playgroud)
这在Ninject邮件列表中得到了解答.
此行为是预期的.注入集合时,它将找到与泛型参数匹配的所有绑定,并将它们添加到正在注入的集合中.如果删除Element上的绑定,则会注入空集合.
另一个例子给出了基于这种行为可以做什么.