Gets a MethodBody that provides access to the MSIL stream, local variables, and exceptions for the current method.
using System;
using System.Reflection;
public class Example
{
public static void Main()
{
MethodInfo mi = typeof(Example).GetMethod("MethodBodyExample");
MethodBody mb = mi.GetMethodBody();
Console.WriteLine("\r\nMethod: {0}", mi);
Console.WriteLine(mb.InitLocals);
Console.WriteLine(mb.MaxStackSize);
foreach (LocalVariableInfo lvi in mb.LocalVariables)
{
Console.WriteLine("Local variable: {0}", lvi);
}
foreach (ExceptionHandlingClause ehc in mb.ExceptionHandlingClauses)
{
Console.WriteLine(ehc.Flags.ToString());
switch (ehc.Flags)
{
case ExceptionHandlingClauseOptions.Filter:
Console.WriteLine(ehc.FilterOffset);
break;
case ExceptionHandlingClauseOptions.Finally:
break;
default:
Console.WriteLine(ehc.CatchType);
break;
}
}
}
public void MethodBodyExample(object arg)
{
int var1 = 42;
string var2 = "Forty-two";
}
}
Related examples in the same category