Click here to Skip to main content
15,895,256 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 232.1K   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.Collections.Generic;
using System.Text;

namespace CodeAnalysisTools
{
	public enum CodeAnalysisActivityType
	{
		None,
		FindNotUsed,
		FindDuplicateMethods,
		FindCircularDependencies,		
		FindSubClasses,
		GetNamespaces
	}

	public abstract class CodeAnalysisActivity
	{
		public delegate void OnEventCallback(object[] args, CodeAnalysisActivityType taskType);
		public delegate void OnProgressUpdate(int total, int current);

		private OnEventCallback onEventCallback = null;
		private OnProgressUpdate onProgressUpdate = null;
		private CodeAnalysisActivityType thisTaskType = CodeAnalysisActivityType.None;

		internal CodeAnalysisActivity(OnEventCallback onEventCallback, OnProgressUpdate onProgressUpdate, CodeAnalysisActivityType taskType)
		{	
			this.onEventCallback = onEventCallback;
			this.onProgressUpdate = onProgressUpdate;
			this.thisTaskType = taskType;
		}

		public abstract void Run(object[] args);

		protected void CallProgressUpdate(int total, int current)
		{
			if (this.onProgressUpdate != null)
				this.onProgressUpdate(total, current);
		}

		protected void CallEventCallback(object[] args)
		{
			if (this.onEventCallback != null)
				this.onEventCallback(args, thisTaskType);
		}

		public static CodeAnalysisActivity CreateTask(CodeAnalysisActivityType taskType, OnEventCallback onEventCallback, OnProgressUpdate onProgressUpdate)
		{
			switch (taskType)
			{
				case CodeAnalysisActivityType.FindNotUsed:
					throw new NotImplementedException("Invalid Analysis Task Requested");					
					//return new FindNotUsed(onEventCallback, onProgressUpdate);

				case CodeAnalysisActivityType.FindDuplicateMethods:
					return new FindDuplicateMethods(onEventCallback, onProgressUpdate);

				case CodeAnalysisActivityType.FindCircularDependencies:
					throw new NotImplementedException("Invalid Analysis Task Requested");					
					//return new FindCircularDependencies(onEventCallback, onProgressUpdate);				

				case CodeAnalysisActivityType.GetNamespaces:
					throw new NotImplementedException("Invalid Analysis Task Requested");					
					//return new GetNamespaceHierarchy(onEventCallback, onProgressUpdate);					

				case CodeAnalysisActivityType.FindSubClasses:
					throw new NotImplementedException("Invalid Analysis Task Requested");										

				default:
					throw new ArgumentException("Invalid Analysis Task Requested");					
			}
		}
	}
}

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