Using Ninject 2.2, I have the following failing test (simplified):
public interface IGenericView<T>
{
}
public interface IDefaultConvention
{
}
public class DefaultConvention : IDefaultConvention
{
}
public class DefaultConventionView : IGenericView<IDefaultConvention>
{
}
public class StringView : IGenericView<string>
{
}
[TestFixture]
public class NinjectTests
{
private static IKernel _kernel;
[SetUp]
public void Setup()
{
_kernel = new StandardKernel();
}
[Test]
public void CanResolveAllClassesClosingOpenGenericInterface()
{
// Arrange
_kernel.Bind<IDefaultConvention>().To<DefaultConvention>();
_kernel.Scan(
x =>
{
x.FromCallingAssembly();
x.BindWith(new GenericBindingGenerator(typeof(IGenericView<>)));
});
// Act
object target1 = _kernel.Get<IGenericView<IDefaultConvention>>();
object target2 = _kernel.Get<IGenericView<string>>();
// Assert
Assert.IsAssignableFrom<DefaultConventionView>(target1);
Assert.IsAssignableFrom<StringView>(target2);
Assert.AreEqual(2, _kernel.GetAll(typeof(IGenericView<>)).Count()); // Always returns 0
}
}
The first two assertions pass, so I know the types themselves are being bound correctly, but I can't retrieve all the bindings for the open generic interface like I want to. Is this at all possible?