private void CompareWithCustomComparer(object object1, object object2, string breadCrumb, Type t1, Delegate customComparer) { if (TypesToIgnore.Contains(t1)) { return; } CustomComparerDelegateWithDifferenceRecording customComparerDelegateWithDifferenceRecording = customComparer as CustomComparerDelegateWithDifferenceRecording; CustomComparerDelegate customComparerDelegate = customComparer as CustomComparerDelegate; if (customComparerDelegateWithDifferenceRecording != null) { // Compare with a custom comparator List <string> differences = customComparerDelegateWithDifferenceRecording.Invoke(object1, object2); if (differences.Count > 0) { Differences.AddRange(differences); } } else { if (customComparerDelegate != null) { // Compare with a custom comparator if (!customComparerDelegate.Invoke(object1, object2)) { Differences.Add(string.Format("object1{0} != object2{0} ({1},{2})", breadCrumb, object1, object2)); } } else { throw new InvalidOperationException("Bad custom comparer specified."); } } if (Differences.Count >= MaxDifferences) { return; } }
/// <summary> /// Adds the custom comparer. /// </summary> /// <param name="type">The type.</param> /// <param name="customComparer">The custom comparer.</param> public void AddCustomComparerWithDifferenceRecording(Type type, CustomComparerDelegateWithDifferenceRecording customComparer) { _customComparers.Add(type, customComparer); }