Click here to Skip to main content
15,897,371 members
Articles / Desktop Programming / Windows Forms

Diagrams with Reflector and the Graph Plug-in (Part 2)

Rate me:
Please Sign up or sign in to vote.
4.27/5 (7 votes)
23 Feb 2009CPOL6 min read 35.2K   1.1K   38  
Graphing other dependencies, without Reflector now.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;

using Refractor.Common;
using SDILReader;

namespace Refractor.Plugins.ILDiagrams
{
    public class GeneralClassesGb : BaseILGraphBuilder
    {
        public override void Translate()
        {
            this._ignoreSidePanelItems = false;

            // Build the graph nodes and edges.
            FirstPass();

            // Create the linkage.
            SecondPass();
        }

        protected override string GetEmptyText()
        {
            string result = " Empty Graph ";

            if (this._hasOrphans)
                result = " Empty Graph \n (has orphans)";
            else
                result = " Empty Graph ";

            return result;
        }

        private Dictionary<string, TypeItem> _types = new Dictionary<string, TypeItem>();

        private void FirstPass()
        {
            foreach (TypeItem item in this._sidePanel.GetItems())
            {
                if (CheckWorker()) break;

                Type t = item.TypeRef;

                // Get the full name of the type. (shortID)
                string id = t.FullName;

                // Add the graph node with a link back to the type.
                this.AddNode(id, t.Name, _sharedOptions.TypeColor, item);

                // Hang onto the type and the corresponding graph node.
                _types.Add(id, item);
            }
        }

        private void SecondPass()
        {
            foreach (KeyValuePair<string, TypeItem> pair in _types)
            {
                if (CheckWorker()) break;

                foreach (MethodItem m in pair.Value.Methods)
                {
                    ParseInstructions(m.MethodInfo, pair.Key);
                }
            }
        }

        private void ParseInstructions(MethodInfo mi, string fromId)
        { 
            MethodBodyReader mr = GetMethodBodyReader(mi);
            if (mr == null) return;
            if (mr.instructions == null) return;

            foreach (ILInstruction instruction in mr.instructions)
            {
                if (CheckWorker()) break;
                if (instruction.Operand == null) continue;
                if (instruction.Code.OperandType != OperandType.InlineMethod) continue;

                Type declType;
                string name, calledId;
                GetInstrDetails(instruction, out name, out declType, out calledId);
                if (name == null) continue;

                calledId = declType.FullName;
                BaseItem calledItem = _projectBrowser.Lookup(calledId);
                
                if (calledItem == null) continue;

                if (_addedNodes.ContainsKey(calledId))
                {
                    AddEdge(fromId, calledId, EdgeStyle.NormalArrow);
                }
            }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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