lundi 12 mars 2018

Getting the Local Variables, ExceptionHandlingClauses and IL array from a dynamic method

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?





Aucun commentaire:

Enregistrer un commentaire