473,388 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Using Reflection to acquire IL Code

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
2 2577
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
2
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
0
by: Marco Segurini | last post by:
Hi, I am trying to dynamically install/deinstall a message handler to a System.Windows.Forms.Form using NativeWindow. I do not use IMessageFilter derived class because it intercept only the...
2
by: Mark | last post by:
Am I out of my mind if I use Reflection everytime someone logs into our site to get and track the current Major/Minor/Build/Revision version that the person is viewing our site through? This...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
2
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article...
2
by: diego | last post by:
hi everyone, i have a sub that opens a form given the form's name as string and opens it using System.Reflection. How can I set the form's properties at runtime. Here is my code. Public Sub...
4
by: Per Bolmstedt | last post by:
(This question has been asked previously in this group, but I don't think it was ever really properly answered.) I want to use reflection - preferably - to find all Web Forms in my web site that...
10
by: Dmitriy V'jukov | last post by:
On 16 ΝΑΚ, 21:41, "Dmitriy V'jukov" <dvyu...@gmail.comwrote: According to definition, release operation on 'std::atomic_global_fence_compatibility' can only 'synchronize with' acquire...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.