-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
System.Reflection.Emit can't call a generic method having a function pointer parameter #100020
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection-emit |
The method
uses an unnecessary generic. Changing that to
and
to
works for me. |
@steveharter, I think the principle here is that the code should work regardless of the generic being unnecessary. A more complex example that uses the generic could be created and the same issue would still exist, that the API cannot be called. Consider for example: public static unsafe T M1<T>(delegate*<T> func) => func(); |
This is indeed an issue, but moving to v10. The workaround is to use
The issue is the code in |
See also #75348 for other remaining function pointer work around Invoke. |
Description
It isn't possible to emit a dynamic assembly using
System.Reflection.Emit
that emits a call to a generic method having a function pointer as an argument. The code fails with aArgumentNullException
Reproduction Steps
This program emits a method equivalent to the following C# code:
Expected behavior
The method is correctly emitted and runs.
Actual behavior
The
EmitCall()
fails with aArgumentNullException
.Stack trace:
Regression?
Technically in .NET 7 the emit phase works, which now fails in .NET 8.
That doesn't really matter since the emitted code can't run in .NET 7: it searches for a
C.M1(IntPtr)
overload, which doesn't exist.Known Workarounds
Using
IntPtr
instead of a function pointer parameter works.Configuration
.NET 8.0.2
Windows 11 22631.3296
x64
Other information
Remarks:
DynamicMethod
instead of a dynamic assembly.The
GetTypeRefNested
method probably isn't meant to be called for function pointers, as it seems to be assuming that a proper type (not a function pointer) was passed, here:runtime/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.cs
Line 208 in ffe7e26
FullName
isnull
for a function pointer, causing theGetTypeRef
to fail:runtime/src/coreclr/vm/commodule.cpp
Line 42 in ffe7e26
The text was updated successfully, but these errors were encountered: