Add function pointer support for managed instance methods #8709
-
Currently only managed static methods are supported. Besides the obvious feature benefit of supporting instance methods, this feature would help with re-use of a signature across different types and methods including using "System.Object" for all reference types: public class TestClass1
{
public TestClass1 SomeMethod => this; // We want to return a reference type; just return 'this'.
}
public class TestClass2
{
public TestClass2 SomeMethod=> this;
}
internal unsafe class Program
{
static void Main()
{
// Common signature that takes a reference type as "this" and returns a reference type.
// Note the use of the 'instance' keyword.
delegate* instance<object, object> fObjectObject;
{
IntPtr fp = typeof(TestClass1).GetMethod("get_SomeMethod").MethodHandle.GetFunctionPointer();
fObjectObject = (delegate* instance<object, object>)fp;
TestClass1 ret = (TestClass1)fObjectObject(new TestClass1());
}
{
IntPtr fp = typeof(TestClass2).GetMethod("get_SomeMethod").MethodHandle.GetFunctionPointer();
fObjectObject = (delegate* instance<object, object>)fp;
TestClass2 ret = (TestClass2)fObjectObject(new TestClass2());
}
}
} This feature would be leveraged by reflection invoke as a fast path that avoids emit (where reflection creates and jits expensive DynamicMethods) for common cases by re-using hard-coded function pointer signatures that would help with startup and warmup performance. The original function pointer proposal at https://github.com/dotnet/csharplang/blob/main/proposals/csharp-9.0/function-pointers.md#allow-instance-methods discussed this feature; some snippets:
|
Beta Was this translation helpful? Give feedback.
#7331