Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / MSIL

Code Analysis Tools - A collection of IL code analysis tools

Rate me:
Please Sign up or sign in to vote.
4.96/5 (41 votes)
1 Sep 2006CPOL24 min read 230.3K   1.6K   138  
This tool analyses the IL of a list of assemblies, looking for types, methods, and fields that are not used by another list of assemblies. This lets you see if you have unused legacy code lying around that should be cleaned up.
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Microsoft.Cci;
namespace CodeAnalysisTools
{
	public enum Visibility
	{
		DontWorryBoutIt,
		Private,
		//Protected,		
		Internal,
		//ProtectedInternal,		
		Public
	}

    public abstract class MyXNode
    {
        private Dictionary<string, MyTypeNode> callers = new Dictionary<string, MyTypeNode>();
        public Dictionary<string, MyTypeNode> Callers
        {
            get { return callers; }
            set { callers = value; }
        }

        public void AddToCallers(TypeNode typeNode)
        {
            if (!callers.ContainsKey(typeNode.FullName))
            {
                callers.Add(typeNode.FullName, new MyTypeNode(typeNode));
            }
        }

		public abstract string FullName
		{
			get;
		}

		public abstract string Name
		{
			get;
		}

		public abstract Visibility Visibility
		{
			get;
		}

		public abstract Visibility CheckVisibility();
    }

    public class MyTypeNode : MyXNode
	{
		public TypeNode thisType;
		public MyTypeNode baseType;
		public List<MyTypeNode> subTypes = new List<MyTypeNode>();        

		public MyTypeNode(TypeNode type)
		{
			this.thisType = type;
		}

		public override string FullName
		{
			get { return this.thisType.FullName; }
		}

		public override string Name
		{
			get { return this.thisType.Name.Name; }
		}

		public override Visibility Visibility
		{
			get
			{
				if (thisType.IsPublic)
					return Visibility.Public;
				if (thisType.IsAssembly)
					return Visibility.Internal;				
				if (thisType.IsPrivate)
					return Visibility.Private;

				return Visibility.DontWorryBoutIt;

				//if (thisType.IsFamily)
				//    return Visibility.Protected;
				//if (thisType.IsFamilyOrAssembly)
				//	return Visibility.ProtectedInternal;
			}
		}

		public override Visibility CheckVisibility()
		{
			//check for internal recomendation		
			bool anyExternalCaller = false;				
			if (this.Visibility > Visibility.Internal)
			{
				foreach (MyTypeNode caller in this.Callers.Values)
				{
					if (caller.thisType.DeclaringModule.Name != this.thisType.DeclaringModule.Name)
					{
						anyExternalCaller = true;
						break;
					}
				}

				if (!anyExternalCaller)
				{
					return Visibility.Internal;
				}
			}			

			return Visibility.DontWorryBoutIt;
		}

		public override string ToString()
		{
			return this.FullName;
		}
        
		public override int GetHashCode()
		{
			return this.FullName.GetHashCode();
		}        
	}

    public class MyMethodNode : MyXNode
    {
        public Method method;
        public MyTypeNode baseType;
        public List<MyTypeNode> subTypes = new List<MyTypeNode>();        

        public MyMethodNode(Method method)
        {
            this.method = method;
        }

		public override string FullName
        {
            get { return this.method.FullName; }
        }

		public override string Name
        {
            get { return this.method.Name.Name; }
        }

		public override Visibility Visibility
		{
			get
			{
				if (method.IsPublic)
					return Visibility.Public;
				if (method.IsAssembly)
					return Visibility.Internal;
				if (method.IsPrivate)
					return Visibility.Private;

				return Visibility.DontWorryBoutIt;

				//if (thisType.IsFamily)
				//    return Visibility.Protected;
				//if (thisType.IsFamilyOrAssembly)
				//	return Visibility.ProtectedInternal;
			}
		}

		public override Visibility CheckVisibility()
		{
			//check for private recomendation
			if (this.Visibility > Visibility.Private)
			{
				//check to see if all callers are in the same type as field
				bool anyExternalCaller = false;
				foreach (MyTypeNode caller in this.Callers.Values)
				{
					if (caller.FullName != this.method.DeclaringType.FullName)
					{
						anyExternalCaller = true;
						break;
					}
				}

				if (!anyExternalCaller)
				{
					return Visibility.Private;
				}
				else
				{
					////check for internal recomendation									
					if (this.Visibility > Visibility.Internal)
					{
						foreach (MyTypeNode caller in this.Callers.Values)
						{
							if (caller.thisType.DeclaringModule.Name != this.method.DeclaringType.DeclaringModule.Name)
							{
								anyExternalCaller = true;
								break;
							}
						}
					}

					if (!anyExternalCaller)
					{
						return Visibility.Internal;
					}
				}
			}

			return Visibility.DontWorryBoutIt;
		}

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

        public override int GetHashCode()
        {
            return this.FullName.GetHashCode();
        }
    }

    public class MyFieldNode : MyXNode
    {
        public Field field;
        public MyTypeNode baseType;
        public List<MyTypeNode> subTypes = new List<MyTypeNode>();

        public MyFieldNode(Field field)
        {
            this.field = field;
        }

		public override string FullName
        {
            get { return this.field.FullName; }
        }

		public override string Name
        {
            get { return this.field.Name.Name; }
        }

		public override Visibility Visibility
		{
			get
			{
				if (field.IsPublic)
					return Visibility.Public;
				if (field.IsAssembly)
					return Visibility.Internal;
				if (field.IsPrivate)
					return Visibility.Private;

				return Visibility.DontWorryBoutIt;

				//if (thisType.IsFamily)
				//    return Visibility.Protected;
				//if (thisType.IsFamilyOrAssembly)
				//	return Visibility.ProtectedInternal;
			}
		}

		public override Visibility CheckVisibility()
		{
			//check for private recomendation
			if (this.Visibility > Visibility.Private)
			{
				//check to see if all callers are in the same type as field
				bool anyExternalCaller = false;
				foreach (MyTypeNode caller in this.Callers.Values)
				{
					if (caller.FullName != this.field.DeclaringType.FullName)
					{
						anyExternalCaller = true;
						break;
					}
				}

				if (!anyExternalCaller)
				{
					return Visibility.Private;			
				}
				else
				{
					////check for internal recomendation									
					if (this.Visibility > Visibility.Internal)
					{
						foreach (MyTypeNode caller in this.Callers.Values)
						{
							if (caller.thisType.DeclaringModule.Name != this.field.DeclaringType.DeclaringModule.Name)
							{
								anyExternalCaller = true;
								break;
							}
						}
					}

					if (!anyExternalCaller)
					{
						return Visibility.Internal;
					}
				}
			}

			return Visibility.DontWorryBoutIt;
		}

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

        public override int GetHashCode()
        {
            return this.FullName.GetHashCode();
        }
    }
}

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
United States United States
I have been a professional developer since 1996. My experience comes from many different industries; Data Mining Software, Consulting, E-Commerce, Wholesale Operations, Clinical Software, Insurance, Energy.

I started programming in the military, trying to find better ways to analyze database data, eventually automating my entire job. Later, in college, I automated my way out of another job. This gave me the great idea to switch majors to the only thing that seemed natural…Programming!

Comments and Discussions