Search code examples
c#genericsgeneric-type-argumentnested-generics

c# compiler doesn't resolve generic type parameters by constraints


I have the following use case:

I have created generic-endpoint strucure in my ASP.NET Core WebAPi project.

And now I want to write an extention method to ServiceCollection to register my Endpoints to DI easily.

Now I will try to demonstrate what I want. Let's say we have:

interface IEndPoint<TInput, TOutput>
{
    TOutput Execute(TInput input);
}

class StrToIntEndPoint : IEndPoint<string, int>
{
    public int Execute(string input)
    {
        return int.Parse(input);
    }
}

class ServiceCollection { }

static class ServiceCollectionServiceExtensions
{
    public static void AddScoped<TService, TImplementation>(this ServiceCollection services) where TImplementation : TService
    {

    }
}

static class MyCustomExt
{
    // Here, I have to add TInput, TOutput type parameters to AddEndpoint method too.
    public static void AddEndpoint<T, TInput, TOutput>(this ServiceCollection services) where T: IEndPoint<TInput, TOutput>
    {
        services.AddScoped<IEndPoint<TInput, TOutput>, T>();
    }
}

class Program
{
    static void Main()
    {
        var services = new ServiceCollection();

        // Compile Error: Incorrect number of type parameters
        services.AddEndpoint<StrToIntEndPoint>();

        // fine
        services.AddEndpoint<StrToIntEndPoint, string, int>();

    }
}

My question is why compiler doesn't resolve string and int as type parameters for AddEndpoint via refering to StrToIntEndPoint?


Solution

  • Sadly it’s just not the way generic constraints/arguments work. You either specify NO generic type arguments and the compiler infers all arguments, or you specify all of them.