Ninject injection adds an item to the collection when creating the collection

I use MVVM light and set the binding as follows:

class TestModule:NinjectModule { public override void Load() { Bind<ICollection<Element>>().To<Collection<Element>>(); Bind<Element>().ToSelf(); } } 

When I try to get an ICollection, I get a collection with an ONE element. I am expecting an exmpty collection.

  var _kernel = new StandardKernel(new TestModule()); var col = _kernel.Get<ICollection<Element>>(); Console.WriteLine("Count={0}", col.Count); //Write "Count=1", Expect "Count=0" 
+4
source share
1 answer

This is a response to the Ninject mailing list .

This behavior is expected. When the collection is entered, it will find all the bindings that match the general parameter and add them to the collection being injected. If you unlink the element, an empty collection will be injected.

The following is an example showing what can be done based on this behavior.

+5
source

All Articles