Click here to Skip to main content
15,896,111 members
Articles / Programming Languages / C#

Bend the .NET Object to your Will!

Rate me:
Please Sign up or sign in to vote.
4.83/5 (25 votes)
14 Jan 2009CPOL4 min read 47K   123   70  
Clone, serialize, and deep-compare any .NET object, regardless of type
using System.Collections.Generic;
using System.Linq;

namespace CodeProject.Batte.ObjectMastery
{
	/*
	 * GraphRegistry.cs
	 * Author: John Batte
	 * Last Modified: 1/5/2009
	 * See http://www.codeproject.com/info/cpol10.aspx for license details
	 */

	/// <summary>
	/// Provides a reference cache for identifying previously enumerated
	/// object instances within the <see cref="ObjectGraph"/>.
	/// </summary>
	internal class GraphRegistry
	{
		private readonly IDictionary<int, IDictionary<object, ObjectGraph>> _registry;

		/// <summary>
		/// Initializes a new instance of the <see cref="GraphRegistry"/> class.
		/// </summary>
		public GraphRegistry()
		{
			_registry = new Dictionary<int, IDictionary<object, ObjectGraph>>();
		}

		/// <summary>
		/// Registers or identifies the target reference.
		/// </summary>
		/// <param name="target">The object reference to register.</param>
		/// <param name="graph">The object graph requesting the registration.</param>
		/// <returns>
		/// Null if the target has not been previously registered; otherwise,
		/// a reference to the <see cref="ObjectGraph"/> that originally registered the target.
		/// </returns>
		public ObjectGraph Register(object target, ObjectGraph graph)
		{
			// I'd rather not do a brute force scan of all previously
			// registered objects in the graph... and I can't rely on
			// GetHashCode across multiple types / instances... so I'm 
			// using one to short circuit the other.  Sort of like putting
			// an index on a checksum column.  Doesn't guarantee uniqueness
			// but it gets us close.

			int key = target.GetHashCode();

			IDictionary<object, ObjectGraph> hashMatches = _registry.ContainsKey(key)
			                                               	? _registry[key] ?? new Dictionary<object, ObjectGraph>()
			                                               	: new Dictionary<object, ObjectGraph>();

			ObjectGraph existingGraph = (from match in hashMatches
			                             where ReferenceEquals(match.Key, target)
			                             select match.Value).FirstOrDefault();

			if (existingGraph != null) return existingGraph;

			hashMatches[target] = graph;
			_registry[key] = hashMatches;
			return null;
		}

		/// <summary>
		/// Clears the registry of all object references.
		/// </summary>
		public void Clear()
		{
			foreach (var key in _registry.Keys) if (_registry[key] != null) _registry[key].Clear();
			_registry.Clear();
		}
	}
}

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
United States United States
I'm a professional developer with over 9 years of experience in advanced C# development. I've worked extensively with every phase of the SDLC and have developed and deployed many enterprise solutions using the latest .Net technologies. Please let me know if you have any suggestions or questions.

Comments and Discussions