About one year ago I wrote a small library which had as sole purpose the interpretation of the MethodBody.

What if this usefull for? Well using standard reflection mechanism from .NET one can find out from an Assembly what modules are inside, what types (classes, interfaces, enums, etc) and for each one can get the definition of the methods, fields and so on. Unfortunately when you get to retrieve the body of a MethodBase you stumble into a deadend. Microsoft does provide just a single method that can help: MethodBase.GetMethodBody. This returns an array of bytes that represent the body of the method. How in the nine hells can this be interpreted? We might want to know if a certain method is called from another method. By looking at an array of byte this task is dauting.

Still, if one looks at Lutz Roeder’s Reflector one can see that you can interpret this and even retrieve actual C# code from it. I managed to do something similar and the library I mentioned is a lightweight tool that creates a set of Intermediate Language instructions from the body of a method by looking at that array. I won’t get into details here because it is described pretty well in the codeproject article posted by me: http://www.codeproject.com/csharp/sdilreader.asp.

Check it out

Leave a Reply