By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

Using Reflection to acquire IL Code

P: n/a
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
Aug 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
The MethodBody class in 2.0 gives you access to the IL
http://msdn2.microsoft.com/en-us/library/28ec3e6z.aspx/
http://blogs.msdn.com/haibo_luo/arch...02/476242.aspx includes a
full example of parsing/formatting that IL

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

"Nate" <Na**@discussions.microsoft.comwrote in message
news:AC**********************************@microsof t.com...
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

Aug 2 '06 #2

P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nate" <Na**@discussions.microsoft.comwrote in message
news:AC**********************************@microsof t.com...
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

Aug 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.