Codeplex library to compare .NET objects can be found here
Here is a sample usage of the ObjectCompare class:
Here is a sample usage of the ObjectCompare class:
// An equality comparator for XXX. Uses a 3rd party ObjectComparer utility class. // Objects are considered equal if all their public properties and fields are equal // UNUSUALLY ReferenceEquals fails when the 2 references are equal // We should not be comparing the same objects in these // tests static bool XXXsAreEqual(XXX ds1, XXX ds2, List<string> elementsToIgnore = null) { // Check for null values if (ds1 == null) { return (ds2 == null); } else if (ds2 == null) { return false; } // Check for NOT reference equals if (object.ReferenceEquals(ds1, ds2)) return false; // This should NOT happen in these unit tests, fail if it does! // compare run-time types. if (ds1.GetType() != ds2.GetType()) return false; // Ensure all public non-static properties and fields are equal var objectComparator = new ObjectCompare(); objectComparator.CompareStaticFields = false; objectComparator.CompareStaticProperties = false; objectComparator.ComparePrivateProperties = false; objectComparator.ComparePrivateFields = false; objectComparator.CompareFields = true; objectComparator.CompareChildren = true; objectComparator.CompareReadOnly = true; objectComparator.CompareProperties = true; objectComparator.Caching = true; objectComparator.AutoClearCache = true; objectComparator.IgnoreObjectTypes = false; // Already done this above objectComparator.MaxDifferences = 1; objectComparator.ElementsToIgnore = elementsToIgnore ?? new List<string>(); bool res = objectComparator.Compare(ds1, ds2); // If this method fails, check the cause here! List<string> causeOfFailure = objectComparator.Differences; if (causeOfFailure.Count > 0) { Debug.Write("Equality failure caused by following properties/fields: "); string failedOn = string.Join(", ", causeOfFailure.ToArray()); Debug.WriteLine(failedOn); } return res; }To convert this to an equals operator of some kind you would have to fix the reference equals part to return true when the 2 references are equal.
No comments:
Post a Comment