-
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
Proposal: MethodInfo.GetMethodFromFunctionPointer(void*) #36363
Comments
Note that function pointers are not guaranteed to be unique. Calling |
Hey @jkotas - thank you for taking the time to look at this proposal! Well that point in particular just went out the window then. I'm very curious about why that's the case though, is there some resource/specs/etc. that I can use to read up about this? I assumed the function token would be unique for any given method in a loaded assembly 🤔 Putting that specific detail aside (I might just keep using |
The ECMA-335 spec hints at this behavior:
It means that e.g.
The reverse lookup would be slow, and supporting it adds extra constrains on the runtime to support a niche scenario. I do not see it as a good tradeoff. If you want to use reflection, stick with reflection objects, without roundtripping through function pointers. |
I see, yeah that makes perfect sense. Closing this then, have a nice day! |
Overview
Related to the function pointers proposal for C# 9.
This issue is about adding a new API to
System.Reflection.MethodInfo
:This would return the
MethodInfo?
instance mapping to the managedstatic
method being pointed at by thatvoid*
pointer. Users would call this method by passing adelegate*
pointer to astatic
managed method, making sure that the assembly the method belongs to is still loaded into memory. This method would only be used for function pointers pointing tostatic
managed methods.In practice, it would act as a reverse lookup from getting a managed
delegate*
pointer from astatic
method using the&
operator on the target method (through the method group syntax).Sample usage
Motivation/behavior
It might be useful for developers to be able to inspect targeted methods using reflection, and this API would go well along with the other existing similar APIs (eg.
MethodBase.GetMethodFromHandle
).I've included a practical use case example from one of my project just to provide additional context for anyone interested. Putting that into an expandable block to keep the main post more concise.
Practical use case example (tap to expand)
I'm working on a library called
ComputeSharp
, which lets you write DirectX 12 compute shaders in C#, and then converts them into HLSL code and runs them on the GPU. In particular, shaders have the ability to capture functions (pointing to static methods) to enable some sort of metaprogramming. Here's an example of what a simple compute shader might look like:Users might assign that
Activation
from any static method matching the signature.There are two main issues with this approach though, in particular:
GetHashCode
method by callingDelegate.Method.GetHashCode
for each captured function, which is innefficient due to the use of reflection. If I just had a function pointer instead I could just use the pointer value as the identifier for each captured method.Also, being able to just use a function pointer would also skip the delegate allocation entirely, which would not be a huge difference on its own, but still nice to have 😄
The text was updated successfully, but these errors were encountered: