-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to Obtain a Method Pointer Without Reflection in NativeAOT? #100103
Comments
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas |
The C# function pointers (
|
I'm trying to port my code to Unity Il2CPP, but reflection is ruining everything. Is it possible to get a pointer to a method without reflection and without necessarily specifying "static"? |
At the moment my code looks like this. But it only accepts static functions. Is there a way to get a pointer to more than just static methods without reflection? |
Stepping back - once you obtain the function pointer to an instance method, how are you going to invoke it? |
You are not the first person to ask me about this :). I'm not going to call her. I will store the function instructions into a variable as a byte array :). Is there any way to get a pointer to a non-static method? I just need his address. I'm not trying to call him :) |
|
THANK YOU!! |
Remove the unsupported |
I'm also curious about this statement. How do you benefit from NativeAOT for your goal? |
FYI calls on NativeAOT are faster cause they don't go through tiering stubs and such and are just always direct, they're also noticeably faster for indirect calls. |
They are porting their code from Il2CPP (Unity's AOT) to .NET NativeAOT backend, so it fits the bill. |
I want to check the instructions of given methods and therefore I need pointers without reflection. Because of this, my code doesn't work. I already know how to get pointers to static methods without reflection. How do I get a pointer to a non-static method without reflection? Sorry for my English, I translate everything through Google Translator |
I don't know of any way to do this today without reflection. #94975 is one proposal to do so. |
Actually, this seems to work fine right now: unsafe
{
delegate*<C, void> func = &Accessor.M;
func(c);
}
static class Accessor
{
[UnsafeAccessor(UnsafeAccessorKind.Method)]
public extern static void M(C c);
}
public class C {
public void M() {
Console.WriteLine("Hello");
}
} |
Worth noting that this returns a pointer to a wrapper static method, not the method itself. |
You have to use reflection or write IL code by hand. The
Yes, that example returns the address of I think with this we exhausted the list of options. |
Hi!
I am currently exploring NativeAOT for a project that requires high performance and direct method calls without the overhead of reflection. My goal is to obtain a pointer to a specific method in C# to use in an unsafe context, ensuring the most efficient execution possible.
In the traditional .NET runtime, we typically rely on reflection (e.g., MethodInfo.MethodHandle.GetFunctionPointer()) to achieve this, but I understand that reflection might not be fully supported or recommended in NativeAOT for performance and runtime efficiency reasons.
Could you provide guidance or recommendations on how we can obtain a method pointer without relying on reflection within the NativeAOT environment? Are there any specific patterns, practices, or API calls in NativeAOT that facilitate this kind of direct method access?
Any examples or documentation you could point me to would be greatly appreciated.
Thank you for your time and assistance.
The text was updated successfully, but these errors were encountered: