Skip to content

Methods that take a function pointer can't be reflection invoked #85028

Description

@MichalStrehovsky

This works in .NET 7 but throws in .NET 8.

unsafe class Program
{
    static void Main()
    {
        typeof(Program).GetMethod("Fnptr").Invoke(null, new object[] { IntPtr.Zero });
    }

    public static void Fnptr(delegate*<void> d) { }
}

I can't find a place in the design doc (#282) where this would be conscious choice. There's a discussion of a potential System.Reflection.FunctionPointer that would work similar to System.Reflection.Pointer but that is orthogonal - System.Reflection.Pointer and a boxed IntPtr/UIntPtr can be used interchangeably as far as reflection is concerned.

To add to the confusion, if the method returns a delegate*, we do allow reflection invoking it and the result will be a boxed IntPtr. Similarly, if the method takes a delegate* as an out parameter, we will box it as IntPtr.

There's a similar issue with FieldInfo.SetValue for a delegate*-typed field where we do allow IntPtr (but confusingly don't allow UIntPtr).

This inconsistency seems jarring.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions