jab icon indicating copy to clipboard operation
jab copied to clipboard

Generic services with constraints causes a non-compilable code

Open ettud opened this issue 2 years ago • 1 comments

Code for reproduction:

        internal interface IService<T>{} where T : IAnotherService
        class ServiceImplementation<T> : IService<T> where T : IAnotherService {  }
        internal interface IAnotherService{} 
        interface IService1 {}
        class ServiceImplementation : IService1 { }
        class ServiceImplementation2 : IAnotherService { }

        [ServiceProvider]
        [Singleton(typeof(IService<>), typeof(ServiceImplementation<>))]
        [Singleton(typeof(IService1), typeof(ServiceImplementation))]
        [Singleton(typeof(IAnotherService), typeof(ServiceImplementation2 ))]
        partial class Container
        {
        }

Expected result: The code is generated only for IService<IAnotherService>.

Actual result: The code is generated for IService<IAnotherService> and for IService<IService1>. Thus the generated code cannot be compiled since the type 'IService1' cannot be used as type parameter 'T': there is no implicit reference conversion from 'IService1' to 'IAnotherService'.

ettud avatar May 12 '22 10:05 ettud

Hm, I can't repro this one. Are on the latest version?

This is the Program.cs I used.

using Jab;

        var c = new Container();
        c.GetService<IService<IAnotherService>>();
        
 internal interface IService<T> where T : IAnotherService {}
    class ServiceImplementation<T> : IService<T> where T : IAnotherService {  }
    internal interface IAnotherService{} 
    interface IService1 {}
    class ServiceImplementation : IService1 { }
    class ServiceImplementation2 : IAnotherService { }

    [ServiceProvider]
    [Singleton(typeof(IService<>), typeof(ServiceImplementation<>))]
    [Singleton(typeof(IService1), typeof(ServiceImplementation))]
    [Singleton(typeof(IAnotherService), typeof(ServiceImplementation2 ))]
    partial class Container
    {
    }

pakrym avatar May 14 '22 04:05 pakrym