Skip to content

Exception on MakeGenericMethod called on a static interface method from GetInterfaceMap's TargetMethods #90351

@hamarb123

Description

@hamarb123

Description

This issue is for adding the tests, see Other information

When you use MakeGenericMethod on a static generic method, which comes from GetInterfaceMap's TargetMethods, it throws an InvalidOperationException. It doesn't throw when you get the method directly from the type.

Gotta love static interface methods :)

Originally found by @Joe4evr on the C# discord, just thought I'd report it so it doesn't get lost.

Reproduction Steps

using System;
using System.Linq;
using System.Reflection;
public class C
{
    public static void Main()
    {
        var x1 = typeof(C1).GetMethod("I1.A", (BindingFlags)(-1));
        var x2 = typeof(C1).GetInterfaceMap(typeof(I1)).TargetMethods.Single((x) => x.Name == "I1.A"); //doesn't occur on InterfaceMethods
        Console.WriteLine(x1.MakeGenericMethod(typeof(int)).ContainsGenericParameters);
        Console.WriteLine(x2.MakeGenericMethod(typeof(int)).ContainsGenericParameters); //exception is here (on call to MakeGenericMethod)
    }
}

public interface I1
{
    static abstract void A<T>();
}

public class C1 : I1
{
    static void I1.A<T>() { } //doesn't need to be an explicit implementation
}

Expected behavior

Prints:

False
False

Actual behavior

Prints:

False
Exception
System.InvalidOperationException: Void I1.A[T]() is not a GenericMethodDefinition. MakeGenericMethod may only be called on a method for which MethodBase.IsGenericMethodDefinition is true.
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   at C.Main()
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

Regression?

No response

Known Workarounds

No response

Configuration

I tested this in sharplab.io
I have also reproduced locally on macOS 13.4.1 x64 with .NET 7.0.10

Other information

The above described issue was fixed with other changes. This issue is now for adding tests to ensure we do not regress.
I also found another instance of the original issue: #90351 (comment).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions