Using Reflection to acquire IL Code

G

Guest

All,

I've been searching all over Google for a nice code sample explaining how to
use reflection to get the instance of a method or property and then retrieve
just the IL code for that method or property. The overall goal is to be able
to reverse engineer a given assembly and create a diagram of dependencies.
For instance, let's say that method foo() of class bob makes a call to method
bar() of class sally, I would like to be able to map that "dependency" into a
diagram, but the only way I can think of getting to that point is to be able
to retrieve the IL code for method foo() and examine it in order to determine
that it is calling a method bar() of class sally.

Does anyone have any links to good code samples specific to the following or
code snippets of their own that they are willing to post? Any suggestions
are welcome.


Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Nate,

You can use reflection to get the MethodInfo instance for the method,
which you can then use to get the method body (through the GetMethodBody
method). Then, you can call the GetILAsByteArray method on the MethodBody
instance to get the corresponding IL code.

From there, you will have to parse the IL from the byte array on your
own.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top