aspnet / DependencyInjection

[Archived] Contains common DI abstractions that ASP.NET Core and Entity Framework Core use. Project moved to https://github.com/aspnet/Extensions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GetService<T>() does not trigger error message when forgetting to add generic parameter

mjenzen opened this issue · comments

Title

Omitting the generic type parameter on GetService does not trigger an error message like other generic methods.

Functional impact

developer must manually track down error with no compiler assistance.

Minimal repro steps

  1. create a new project
    2.add the following method:
public void DoSomething()
        {
            var provider = new ServiceCollection().BuildServiceProvider();
            provider.GetService<>();
        }
  1. Compile code and verify no error is thrown, but build fails.

Expected result

The Compiler should throw an error on the correct line to inform the developer when a generic constraint is missed.

Actual result

No error is thrown and the build fails.

Further technical details

I am not sure if this is a issue with this project, or perhaps an odd feature I am unaware of, or something in the compiler, but other functions seem to throw an expected error message, so I decided to start here.

Hmm. That's weird. That should be a compiler error. There's nothing to be done about it in this repository.

The question is why the compiler doesn't give an error... Maybe you're bumping into this? dotnet/roslyn#15837

Yeah this is super weird, and it looks exactly like the issue @khellang referenced.

I was wondering if this was the case prior to posting, but ended up posting it here because the following code generates the expected warning on the use of Stringify line

namespace OpenGenericError
{
    public static class extensions
    {
        public static string Stringify<T>(this object obj)
        {
            return obj.ToString();
        }
    }

    public class Class1
    {
        public void DoSomething()
        {
            new object().Stringify<>();

            var provider = new ServiceCollection().BuildServiceProvider();
            provider.GetService<>();
        }
    }
}

If you're running on the latest Visual Studio bits and you can reproduce this, I suggest you file an issue in https://github.com/dotnet/roslyn with as many details as you can 😄