You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
When a delegate is created from a DynamicMethod, the Method property returns a MethodInfo which does not implement CreateDelegate #78365
When a delegate is created from a DynamicMethod, the Method property of the resulting delegate returns a MethodInfo which does not implement CreateDelegate, leading to a NotSupportedException.
DynamicMethod's CreateDelegate implementation sets the Method property of the delegate to the DynamicMethod's RTMethodInfo object, which does not implement CreateDelegate itself. This causes problems when trying to, say, convert a delegate from one type to another type with an identical signature.
The project this was found in has for a long time has a CastDelegate helper to do exactly that. It works flawlessly in all other scenarios, including on Mono (using the Mono BCL).
Reproduction Steps
usingSystem.Reflection.Emit;// CastDelegate works for normal delegatesAa=()=>Console.WriteLine("A");varb=(B)CastDelegate(a,typeof(B));a();b();// now lets try the same thing with a DynamicMethod{vardm=newDynamicMethod("dm1",typeof(void),null);varil=dm.GetILGenerator();il.Emit(OpCodes.Ret);a=(A)dm.CreateDelegate(typeof(A));}// This invocation will throw System.NotSupportedExceptionb=(B)CastDelegate(a,typeof(B));a();b();staticDelegateCastDelegate(Delegatedel,TypedelType){varinvokeList=del.GetInvocationList();if(invokeList.Length==1)returninvokeList[0].Method.CreateDelegate(delType,invokeList[0].Target);for(vari=0;i<invokeList.Length;i++){invokeList[i]=CastDelegate(invokeList[i],delType);}returnDelegate.Combine(invokeList)!;}delegatevoidA();delegatevoidB();
Expected behavior
No exception, and a delegate created just as if it were called on the original DynamicMethod.
Actual behavior
When the delegate was created from a DynamicMethod, CreateDelegate throws an exception even when the call would otherwise be valid.
Regression?
No response
Known Workarounds
It is possible to reflect into the RTMethodInfo to get the DynamicMethod reference back and call CreateDelegate on that.
Configuration
This has been tested on .NET 5, 6, and 7, but likely exists on all versions of .NET Core and likely Framework.
Other information
No response
Reacted by madelson
Activity
ghost added
untriagedNew issue has not been triaged by the area owner
Activity
steveharter commentedon Nov 16, 2022
Verified the repro; thanks. For consistency with other MethodInfos it seems like this is a valid issue. The exception occurs here: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInfo.cs#L29-L30.
Since this is an edge case and not a regression has low priority at this point.
Implement DynamicMethod.RTDynamicMethod.CreateDelegate.