Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

Diagrams with Reflector and the Graph Plugin (Part 1)

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
6 Aug 2007CPOL3 min read 56K   1.8K   42   18
Updating the Graph Plugin to provide a diagram of method dependencies within a class.
Screenshot - method_dependencies.gif

Introduction

Recently a friend banged me over the head with Reflector, I had a play, and discovered the wonderful "Graph Plugin", which draws pictures of software. It didn't quite draw the pictures I wanted, so I've added one, and written this article about how easy it was.

The picture I wanted was methods within a class showing which method called which others. For most classes, this is minimal, but for some classes, especially complex UI classes, a diagram like this can be very useful.

This is Reflector, the MSIL dissassembler, by Lutz Roeder, and this is the Graph Plugin, with the instruction call graph, by Jonathan de Halleux. Reflector is obfuscated, sadly, but the source code is available for the Graph Plugin. It uses QuickGraph and GLEE, which I've included in the download, along with Reflector.

QuickGraph is the underlying graph structure, and Glee provides the viewer control and layered layout engine. The combination works well, in that generally a readable diagram is presented.

Finding Method Calls

When Reflector activates our plugin for a class activeType, thanks to Reflector, we can run over the methods very simply like this :

C#
foreach (IMethodDeclaration method in activeType.Methods)
{
}

For each method above, thanks to the Graph Plugin, we can now run over the instructions in the method, also very simply like this :

C#
InstructionGraph igraph = new InstructionGraph(method);
foreach (IInstruction i in igraph.Body.Instructions)
{
}

We can also then pick out the "call" instructions that we're interested in like this :

C#
FlowToCodeConverter flowConverter = new FlowToCodeConverter();
...
if (flowConverter.Convert(i.Code) !=
    System.Reflection.Emit.FlowControl.Call) continue;

This is the basic mechanism that this modified plugin will use to locate method dependencies. We run over the list of methods in the class twice. Once to see which methods we have defined in the class, and a second time to see which methods call which others. Things are complicated by overloaded methods, which have to be tracked individually, and by property get set methods, which can be shown or hidden.

Background

Note that the code included here is not mine, but belongs to Jonathan de Halleux. All we're doing here is adding another control and using the IL call graph / visitor code he's built. I've also taken the liberty of modifying the selection for the original diagrams to click-select, rather than mouse over select.

There are some issues which would be nice to sort out with the Glee viewer control, which I haven't looked into yet:

  • Zoom seems a little awkward, right click to "go back" might help.
  • I haven't worked out how to adjust node position manually.
  • Placement of disconnected nodes seems a little random.
  • Only worked out how to affect the layout using AspectRatio.

Using the Code

You should be able to run reflector from the extracted bin folder, or take the Reflector.Graph.dll and put it in your Reflector folder, if you have it installed already. When you run Reflector, you may have to View|Addins|Add Reflector.Graph.dll before you'll see the extra right click menu options.

When you're on a class, you should have the right click menu for Method Dependencies, which will open the new control for the selected class. Selecting methods in the diagram should focus the method in the main tree control.

If you're interested, the code is mostly 3 methods in HuffsMethodDeps.cs, which are all commented line by line so you can see what's happening.

Points of Interest

Updating the plugin was surprisingly easy. We pretty much create a control, inform the plugin mechanism, and let it know what sort of item we want it to be available for :

C#
UserControl huffsMethodDeps = new HuffsMethodDeps(serviceProvider);
...
this.windowManager.Windows.Add("Graph.HuffsMethodDeps",
    huffsMethodDeps, "Huffs Internal Functional Dependencies");
...
this.AddCommand("Browser.TypeDeclaration", "Huffs Internal Functional Dependencies",
    new EventHandler(this.HuffsMethodDeps_Click));

After that, we just add the button click and we're away.

C#
private void HuffsMethodDeps_Click(object sender, EventArgs e)
{
    this.windowManager.Windows["Graph.HuffsMethodDeps"].Visible = true;
}

History

  • 06/08/2007 - Article created

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
hughdoar@hotmail.com

Comments and Discussions

 
GeneralDoesn't work with the latest .Net Reflector Pin
torial13-Feb-09 9:50
torial13-Feb-09 9:50 
GeneralRe: Doesn't work with the latest .Net Reflector Pin
hughd23-Feb-09 1:53
hughd23-Feb-09 1:53 
GeneralRe: Doesn't work with the latest .Net Reflector Pin
Tom Bushell9-Jun-09 10:49
Tom Bushell9-Jun-09 10:49 
GeneralOutstanding Pin
Shane Triem21-Aug-07 11:14
Shane Triem21-Aug-07 11:14 
GeneralRe: Outstanding Pin
hughd24-Feb-09 12:17
hughd24-Feb-09 12:17 
General.You need to do something Pin
Sacha Barber6-Aug-07 22:02
Sacha Barber6-Aug-07 22:02 
GeneralRe: .You need to do something Pin
h_d757-Aug-07 2:15
h_d757-Aug-07 2:15 
GeneralRe: .You need to do something Pin
Sacha Barber7-Aug-07 5:01
Sacha Barber7-Aug-07 5:01 
GeneralRe: .You need to do something Pin
hughd7-Aug-07 8:39
hughd7-Aug-07 8:39 
GeneralRe: .You need to do something Pin
Sacha Barber7-Aug-07 9:28
Sacha Barber7-Aug-07 9:28 
GeneralRe: .You need to do something Pin
hughd7-Aug-07 11:36
hughd7-Aug-07 11:36 
GeneralRe: .You need to do something Pin
Sacha Barber8-Aug-07 1:49
Sacha Barber8-Aug-07 1:49 
GeneralGreat addition Pin
sumanr6-Aug-07 19:27
sumanr6-Aug-07 19:27 
GeneralRe: Great addition Pin
h_d757-Aug-07 2:17
h_d757-Aug-07 2:17 
GeneralRe: Great addition Pin
sumanr7-Aug-07 13:42
sumanr7-Aug-07 13:42 
GeneralRe: Great addition Pin
hughd9-Aug-07 8:43
hughd9-Aug-07 8:43 
GeneralRe: Great addition Pin
hughd24-Feb-09 12:16
hughd24-Feb-09 12:16 
GeneralExcellent... Pin
AnandChavali6-Aug-07 19:11
AnandChavali6-Aug-07 19:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.