LightInject
LightInject copied to clipboard
StackOverflowException with composite/contravariance
Hi,
this might be linked to #465.
I'm still trying to get the composition-pattern to work and I realized, that I can get it somewhat to work, if I don't use open generics.
void CompositeTest() {
using (var container = new ServiceContainer()) {
container.Register<Foo>();
container.Register<FooBar>();
container.Register<Bar>();
container.Register<IFoo<A>, Composite<A>>();
container.Register<IFoo<B>, Composite<B>>();
container.GetInstance<IFoo<A>>();
container.GetInstance<IFoo<B>>(); // StackOverflowException
}
}
interface A { }
interface B : A { }
interface IFoo<in T> { }
class Foo : IFoo<A> { }
class FooBar : IFoo<A> { }
class Bar : IFoo<B> { }
class Composite<T> : IFoo<T> {
public Composite(IEnumerable<IFoo<T>> foos) {
}
}
This works perfect, if I remove the in
-Keyword from the IFoo<>
-interface. But then in container.GetInstance<IFoo<B>>()
there's only one instance (Bar
since it's IFoo<B>
). I expect all 3 instances..
My workaround is to not register/use the composite-pattern and use container.GetAllInstances<IFoo<B>>()
instead. That one works with contravariance (returns 3 instances with and 1 without contracvariance).
- Version: 5.3.0
- Platform: WIndows 10 / .net 4.7.3221.0