LightInject icon indicating copy to clipboard operation
LightInject copied to clipboard

StackOverflowException with generic composite

Open WolfgangKluge opened this issue 6 years ago • 3 comments

Hi,

when I try to get the composite pattern to work with generics, I get a StackOverflowException..

void CompositeTest() {
    using (var container = new ServiceContainer()) {
        container.Register<FooA>();
        container.Register<FooB>();
        container.Register<FooC>();
        
        container.Register(typeof(IFoo<>), typeof(Composite<>));
        container.Register<IFoo<string>, StringComposite>();
        container.Register<StringComposite>();

        container.GetAllInstances<StringComposite>(); // StackOverflow-Exception
        container.GetAllInstances<IFoo<string>>(); // StackOverflow-Exception
        container.GetAllInstances<IFoo<int>>(); // StackOverflow-Exception
    }
}


interface IFoo { }
interface IFoo<T> : IFoo { }

class FooA : IFoo<string> { }
class FooB : IFoo<string>, IFoo<int> { }
class FooC : IFoo<int> { }

class Composite<T> : IFoo<T> {
    public Composite(IEnumerable<IFoo<T>> fooList) { }
}

class StringComposite : Composite<string> {
    public StringComposite(IEnumerable<IFoo<string>> fooList) : base(fooList) { }
}
  • Version: 5.3.0
  • Platform: WIndows 10 / .net 4.7.3221.0

What do I miss here?

WolfgangKluge avatar Dec 28 '18 21:12 WolfgangKluge

Ups, I think that's from my tests..

The same error occurs, if I use one of these...

        container.GetInstance<StringComposite>();
        container.GetInstance<IFoo<string>>();
        container.GetInstance<IFoo<int>>();

WolfgangKluge avatar Jan 06 '19 13:01 WolfgangKluge

Thanks for reporting this. Will look into it

seesharper avatar Jan 06 '19 13:01 seesharper

I ran into the same issue, it still presents in 6.3.3. Do you have an understanding on how it could be fixed?

yodasad avatar Jun 17 '20 04:06 yodasad