Created
February 24, 2009 02:59
-
-
Save Benrnz/69373 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Performs a deep compare of an object to another of the same type. | |
/// Each property will be compared only if it is primitive, string, or date. | |
/// Otherwise DeepCompare is recursviely called on the two values for the the properties. | |
/// Only public properties that are declared on the concrete type given are compared | |
/// (ie BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly). | |
/// </summary> | |
/// <param name="thisValue">the instance of the object that DeepCompare was invoked on</param> | |
/// <param name="compareTo">the object to compare <see cref="thisValue"/> with</param> | |
/// <returns>true if they are deemed to be equal, otherwise false.</returns> | |
public static bool DeepCompare(this object thisValue, object compareTo) { | |
foreach (PropertyInfo property in compareTo.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { | |
object expectedValue = property.GetValue(compareTo, new object[] { }); | |
object actualValue = property.GetValue(thisValue, new object[] { }); | |
if (expectedValue == null && actualValue == null) { | |
continue; | |
} else if (expectedValue == null && actualValue != null) { | |
return false; | |
} | |
if (expectedValue.GetType().IsPrimitive) { | |
if (!actualValue.Equals(expectedValue)) { | |
Logger.Log(" {0} not equal to compareTo {1}", actualValue.ToString(), expectedValue.ToString()); | |
return false; | |
} | |
} else if (expectedValue is string) { | |
if (!expectedValue.Equals(actualValue)) { | |
Logger.Log(" {0} not equal to expected {1}", actualValue.ToString(), expectedValue.ToString()); | |
return false; | |
} | |
} else if (expectedValue is DateTime) { | |
if (!expectedValue.Equals(actualValue)) { | |
Logger.Log(" {0} not equal to expected {1}", actualValue.ToString(), expectedValue.ToString()); | |
return false; | |
} | |
} else { | |
// if (expectedValue.GetType().Namespace.StartsWith("UDCWebCimMockTest", StringComparison.OrdinalIgnoreCase)) { | |
expectedValue.DeepCompare(actualValue); | |
// } else { | |
// continue; // framework type | |
// } | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment