public bool AreEqual <T1, T2>(T1 x, T2 y, ref Tolerance tolerance) { Guard.OperationValid(CanCompare(x, y), "Should not be called"); // Issue #70 - EquivalentTo isn't compatible with IgnoreCase for dictionaries DictionaryEntry xDictionaryEntry = (DictionaryEntry)Convert.ChangeType(x, typeof(DictionaryEntry)); DictionaryEntry yDictionaryEntry = (DictionaryEntry)Convert.ChangeType(y, typeof(DictionaryEntry)); var keyTolerance = Tolerance.Exact; return(_equalityComparer.AreEqual(xDictionaryEntry.Key, yDictionaryEntry.Key, ref keyTolerance) && _equalityComparer.AreEqual(xDictionaryEntry.Value, yDictionaryEntry.Value, ref tolerance)); }
public bool AreEqual <T1, T2>(T1 x, T2 y, ref Tolerance tolerance) { var xType = x.GetType(); var yType = y.GetType(); // IDictionary<,> will eventually try to compare its key value pairs when using CollectionTally var keyTolerance = Tolerance.Exact; object xKey = xType.GetProperty("Key").GetValue(x, null); object yKey = yType.GetProperty("Key").GetValue(y, null); object xValue = xType.GetProperty("Value").GetValue(x, null); object yValue = yType.GetProperty("Value").GetValue(y, null); return(_equalityComparer.AreEqual(xKey, yKey, ref keyTolerance) && _equalityComparer.AreEqual(xValue, yValue, ref tolerance)); }
public bool AreEqual <T1, T2>(T1 x, T2 y, ref Tolerance tolerance) { IDictionary xDictionary = (IDictionary)x; IDictionary yDictionary = (IDictionary)y; if (xDictionary.Count != yDictionary.Count) { return(false); } CollectionTally tally = new CollectionTally(_equalityComparer, xDictionary.Keys); tally.TryRemove(yDictionary.Keys); if ((tally.Result.MissingItems.Count > 0) || (tally.Result.ExtraItems.Count > 0)) { return(false); } foreach (object key in xDictionary.Keys) { if (!_equalityComparer.AreEqual(xDictionary[key], yDictionary[key], ref tolerance)) { return(false); } } return(true); }
public bool AreEqual <T1, T2>(T1 x, T2 y, ref Tolerance tolerance) { Type xType = x.GetType(); Type yType = y.GetType(); if (!IsCorrectType(xType) || !IsCorrectType(yType)) { return(false); } int numberOfGenericArgs = xType.GetGenericArguments().Length; if (numberOfGenericArgs != yType.GetGenericArguments().Length) { return(false); } for (int i = 0; i < numberOfGenericArgs; i++) { string propertyName = i < 7 ? "Item" + (i + 1) : "Rest"; object xItem = GetValue(xType, propertyName, x); object yItem = GetValue(yType, propertyName, y); bool comparison = _equalityComparer.AreEqual(xItem, yItem, ref tolerance); if (!comparison) { return(false); } } return(true); }