none
MethodBase.GetMethodBody() fails for GetType() method RRS feed

  • Question

  • I'm loading an assembly and then enumerating the types and methods in that assembly.  For some reason MethodBase.GetMethodBody() fails for my type's GetType() method but works for all others.  Does anyone know why it's failing for GetType()?

    Here is my sample console application:

    using System;
    using System.Reflection;

    public class Application
    {
     private static void Dump(byte[] data)
     {
      string separator = string.Empty;

      Console.WriteLine();
      for (int i = 0; i < data.Length; ++i)
       {
       Console.Write("{0}0x{1:X2}", separator, data[i]);
       separator = " ";
       if (((i + 1) % 8) == 0)
        {
        Console.WriteLine();
        separator = string.Empty;
        }
       else if (i == data.Length - 1)
        Console.WriteLine();
       }
      Console.WriteLine();
     }

     public static void Main(string[] args)
     {
      try
       {
       if (args.Length != 1)
        Console.WriteLine("Missing file name argument.");
       else
        {
        Assembly assembly = Assembly.LoadFrom(args[0]);
        Type[] types = assembly.GetTypes();
        for (int i = 0; i < types.Length; ++i)
         {
         Console.WriteLine("Type: {0}", types[i].Name);
         MemberInfo[] members = types[i].GetMembers(BindingFlags.Public | BindingFlags.NonPublic |
           BindingFlags.Instance | BindingFlags.Static);
         for (int j = 0; j < members.Length; ++j)
          {
          if ((members[j] is MethodBase) &&
            ((((MethodBase) members[j]).Attributes & MethodAttributes.Abstract) == 0))
           {
           Console.WriteLine("Method: {0}", members[j].Name);
           MethodBody body = ((MethodBase) members[j]).GetMethodBody();
           if (body == null)
            {
            Console.WriteLine("Failed getting method body.");
            }
           else
            {
            byte[] il = body.GetILAsByteArray();
            Dump(il);
            }
           }
          else if ((members[j] is PropertyInfo) &&
            (((PropertyInfo) members[j]).CanRead ||
            ((PropertyInfo) members[j]).CanWrite))
           {
           Console.WriteLine("Property: {0}", members[j].Name);
           if (((PropertyInfo) members[j]).CanRead)
            {
            Console.WriteLine("get");
            MethodInfo method = ((PropertyInfo) members[j]).GetGetMethod();
            MethodBody body = method.GetMethodBody();
            byte[] il = body.GetILAsByteArray();
            Dump(il);
            }
           if (((PropertyInfo) members[j]).CanWrite)
            {
            Console.WriteLine("set");
            MethodInfo method = ((PropertyInfo) members[j]).GetSetMethod();
            MethodBody body = method.GetMethodBody();
            byte[] il = body.GetILAsByteArray();
            Dump(il);
            }
           }
          }
         }
        }
       }
      catch(Exception e)
       {
       Console.WriteLine("Failed inspecting assembly due to the following error: {0}.",
         e.ToString());
       }
     }
    }

    Thanks,
    Nick

    Tuesday, January 12, 2010 8:33 PM

Answers