Ninject GetAll return duplicate object
I have this code :
abstract class GenericAbstractClass<T> where T : struct { }
class ImplementationClass : GenericAbstractClass<int> { }
class Program {
static void Main (string[] args)
{
IKernel kernel = new StandardKernel();
kernel.Bind(typeof(GenericAbstractClass<>)).To(typeof(ImplementationClass));
var classes = kernel.GetAll(typeof(GenericAbstractClass<>));
Console.WriteLine(classes.Count()); // Print 2.
foreach (var cls in classes) {
if (cls is ImplementationClass)
Console.WriteLine("cls is ImplementationClass");
}
Console.ReadLine();
}
}
The output is :
2
cls is ImplementationClass
cls is ImplementationClass
I expect classes.Count()
result is 1.
Why GetAll
return duplicate object despite of I only bind GenericClass
to ImplementationClass
?
What should I do to make GetAll
return all non-duplicate object?
P.S. I use ninject 2.2
Your binding does not make any sense. You are binding an open generic type to a closed one. It seems that there is a gap in Ninject's plausibility check for bindings.
Change the binding to
kernel.Bind(typeof(GenericAbstractClass<int>)).To(typeof(ImplementationClass));
kernel.GetAll(typeof(GenericAbstractClass<int>));
精彩评论