Skip to content

UnsafeAccessor ambiguity failure when a generic overload exists with the same signature #119875

@jnm2

Description

@jnm2

Description

There appears to be no way to use UnsafeAccessor to run a nongeneric method when it has a generic overload with the same signature.

This fails in .NET 9 and 10, but succeeds in 8.

Reproduction Steps

using System.Runtime.CompilerServices;

class Program
{
    static void Main()
    {
        // Throws System.Reflection.AmbiguousMatchException: 'Ambiguity in binding of UnsafeAccessorAttribute.'
        Example(null);
    }

    [UnsafeAccessor(UnsafeAccessorKind.StaticMethod)]
    private static extern void Example(ContainingClass? _);
}

class ContainingClass
{
    private static void Example() { }
    private static void Example<T>() { }
}

Expected behavior

The nongeneric method is invoked.

Actual behavior

The runtime throws AmbiguousMatchException.

Regression?

Yes, it worked in 8 and fails in 9 and 10.

Known Workarounds

Use reflection if possible instead of UnsafeAccessor

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions