0

From a methodbase, I need to get an array of the IL instructions, the location and kind of that methods exception handlers and the local variables in that methods.

To do this currently (for non dynamic methods I just do)

            MethodBody pBody = pMethod.GetMethodBody();

            Method = pMethod;
            MethodName = pMethod.Name;
            IsPublic = pMethod.IsPublic;
            IsStatic = pMethod.IsStatic;
            Instructions = pBody.GetILAsByteArray();

            Variables = pBody.LocalVariables.ToArray();

            Module = pMethod.Module;
            MethodInfo pMethodInfo = pMethod as MethodInfo;
            ReturnType = pMethodInfo == null ? typeof(void) : pMethodInfo.ReturnType;

            ExceptionHandlers = pBody.ExceptionHandlingClauses.ToArray();
            Position = 0;

            mhashExceptionParameters = new HashSet<ParameterExpression>();
            mdictLocalExpressions = new Dictionary<LocalVariableInfo, ParameterExpression>();

However, as has been noted before, GetMethodBody() doesn't work for dynamic methods (e.g. a method handle got from compiling a linq expression). I know certain answers here show how to get the IL instructions. But how do I get the other things in a methodbody, such as Variables and Exceptionhandlers?

For reference, see this question where solutions to getting the IL byte array are given. How do I get an IL bytearray from a DynamicMethod?

Nick
  • 920
  • 1
  • 7
  • 21
  • Can you add references to the questions explaining how to get the IL instructions of a dynamic method. – thehennyy Mar 12 '18 at 15:52
  • Added the references! – Nick Mar 12 '18 at 17:50
  • In the same way as in the referenced question you could probably hack your way through the implementation details to get the `MethodBody` using the `InternalMethodHandle`. Let the reference source http://referencesource.microsoft.com/ guide you and try it. – thehennyy Mar 13 '18 at 07:43

0 Answers0