Pointers to methods/delegates can be obtained via GetFunctionPointer()in MethodInfo:
But there are no such method in DynamicMethod. The solution is to call method.GetMethodDescriptor() instead.
This method is internal, so use reflection:
You can now cast it to function pointer:
Assembly
Obtained pointer points not just to metadata. This is a complete machine code. You can call it, pass to external dll or view assembly code!
Note that this method does not provide code length. You can use just a big number like 1024, but there are a chance for AccessViolationException to be thrown.
Microsoft.Diagnostics.Runtime
You can use core dumps to get code (Microsoft.Diagnostics.Runtime nuget package):
This method is slow, doesn’t allow invoke of any methods and doesn’t include dynamic methods.
If you using Linux, make sure that you dispose DataTarget (even if you in Debugger) - it’s creating a complete core dump of a process and loading it to the ram. If you quit app, data will persist in memory - you will need to manually delete it in /tmp folder.