Click here to Skip to main content
15,895,667 members
Articles / Programming Languages / C#

Static Code Analysis

Rate me:
Please Sign up or sign in to vote.
4.97/5 (34 votes)
15 Mar 2010CPOL16 min read 87.1K   1.1K   63  
A static code analyzer building method call networks + sample applications
This article describes the operation of a method-based static code analyzer for .NET that constructs in-memory method call networks of compiled assemblies. You will also see a concrete application of static code analysis to generate a website providing easy insights on a sample application.
using System;
using System.Text;

namespace System.Collections.Generic.Net2
{
	/// <summary>
	/// A collection of unique unordered members.
	/// </summary>
	/// <typeparam name="T">Member type.</typeparam>
	[Serializable]
	[Obsolete("Since .NET 3.5, prefer System.Collections.Generic.HashSet, from System.Core.dll.", false)]
	public class HashSet<T> : ICollection<T>
	{
		private Dictionary<T, object> internalList;

		#region Constructor methods

		public HashSet()
			: this(new DefaultEqualityComparer<T>())
		{ }

		public HashSet(IEqualityComparer<T> customComparer)
		{
			this.internalList = new Dictionary<T, object>(customComparer);
		}

		public HashSet(IEnumerable<T> range)
			: this()
		{
			this.AddRange(range);
		}

		public HashSet(IEnumerable<T> range, IEqualityComparer<T> customComparer)
			: this(customComparer)
		{
			this.AddRange(range);
		}

		#endregion Constructor methods

		#region ICollection<T> Members

		public void Add(T item)
		{
			if (!this.internalList.ContainsKey(item))
				this.internalList.Add(item, null);
		}

		public void Clear()
		{
			this.internalList.Clear();
		}

		public bool Contains(T item)
		{
			return this.internalList.ContainsKey(item);
		}

		public void CopyTo(T[] array, int arrayIndex)
		{
			this.internalList.Keys.CopyTo(array, arrayIndex);
		}

		public int Count
		{
			get { return this.internalList.Count; }
		}

		public bool IsReadOnly
		{
			get { return false; }
		}

		public bool Remove(T item)
		{
			return this.internalList.Remove(item);
		}

		#endregion

		#region IEnumerable<T> Members

		public IEnumerator<T> GetEnumerator()
		{
			return this.internalList.Keys.GetEnumerator();
		}

		#endregion

		#region Additional Members

		public void AddRange(IEnumerable<T> range)
		{
			foreach (T item in range)
				this.Add(item);
		}

		#endregion

		#region IEnumerable Members

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return ((System.Collections.IEnumerable)this.internalList.Keys).GetEnumerator();
		}

		#endregion

		#region Default Comparer

		[Serializable]
		private class DefaultEqualityComparer<U> : IEqualityComparer<U>
		{
			public bool Equals(U x, U y)
			{
				if (Object.ReferenceEquals(x, y))
					return true;
				else if (Object.ReferenceEquals(x, null))
					return false;
				else
					return x.Equals(y);
			}

			public int GetHashCode(U obj)
			{
				return obj.GetHashCode();
			}
		}

		#endregion
	}
}

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
Architect AREBIS
Belgium Belgium
Senior Software Architect and independent consultant.

Comments and Discussions