Click here to Skip to main content
15,891,409 members
Articles / Programming Languages / C#

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 56.2K   1.8K   42  
Updating the Graph Plugin to provide a diagram of method dependencies within a class.
namespace Reflector.Graph.Graphs
{
	using System;
	using System.IO;
	using System.Globalization;
	using Reflector.CodeModel;

	internal sealed class StatementGraphFormatter : IFormatter
    {
        private StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
        private bool allowProperties = false;
        private bool newLine = false;
        private int indent = 0;

        public override string ToString()
        {
            return this.writer.ToString();
        }

        public void Write(string text)
        {
            this.ApplyIndent();
            this.writer.Write(text);
        }

        public void WriteDeclaration(string text, object target)
        {
            this.WriteDeclaration(text);
        }

        public void WriteDeclaration(string text)
        {
            this.WriteBold(text);
        }

        public void WriteComment(string text)
        {
            this.WriteColor(text, (int)0x808080);
        }

        public void WriteLiteral(string text)
        {
            this.WriteColor(text, (int)0x800000);
        }

        public void WriteKeyword(string text)
        {
            this.WriteColor(text, (int)0x000080);
        }

        public void WriteIndent()
        {
            this.indent++;
        }

        public void WriteLine()
        {
            this.writer.Write("\\n");
            this.newLine = true;
        }

        public void WriteOutdent()
        {
            this.indent--;
        }

        public void WriteReference(string text, string toolTip, Object reference)
        {
            this.ApplyIndent();
            this.writer.Write(text);
        }

        public void WriteProperty(string propertyName, string propertyValue)
        {
            if (this.allowProperties)
            {
                throw new NotSupportedException();
            }
        }

        public bool AllowProperties
        {
            set
            {
                this.allowProperties = value;
            }

            get
            {
                return this.allowProperties;
            }
        }

        private void WriteBold(string text)
        {
            this.ApplyIndent();
            this.writer.Write(text);
        }

        private void WriteColor(string text, int color)
        {
            this.ApplyIndent();
            this.writer.Write(text);
        }

        private void ApplyIndent()
        {
            if (this.newLine)
            {
                for (int i = 0; i < this.indent; i++)
                {
                    this.writer.Write("    ");
                }

                this.newLine = false;
            }
        }
    }
}

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