private static bool Match(object originalValue, object copyValue) { // If a custom IMatchable description exists use it to determine if there is a match IMatchable originalMatch = originalValue as IMatchable; IMatchable copyMatch = copyValue as IMatchable; if (originalMatch != null && copyMatch != null) { bool res = originalMatch.Matches(copyMatch); return(res); } // Strings are enumerable, so test them first, since string.Equals should be faster than // cycling through each character. string origString = originalValue as string; if (origString != null) { string mString = copyValue as string; if (mString == null) { return(false); } return(origString.Equals(mString)); } // If the object is an enumeration, test the members of the enumeration. // If any members fail the match test, then the whole collection fails the match. IEnumerable originalList = originalValue as IEnumerable; if (originalList != null) { IEnumerable copyList = copyValue as IEnumerable; if (copyList != null) { IEnumerator e = copyList.GetEnumerator(); e.MoveNext(); foreach (object originalItem in originalList) { if (Match(originalItem, e.Current) == false) { return(false); } e.MoveNext(); } } return(true); } if (originalValue == null && copyValue == null) { return(true); } // If the objects are not collections and are not IMatchable, the only remaining thing we can // realistically test is simple equality. // Don't use == here! It will always be false for boxed value types. return(originalValue != null && (originalValue.Equals(copyValue))); }
private static bool Match(object originalValue, object copyValue) { // If a custom IMatchable description exists use it to determine if there is a match IMatchable originalMatch = originalValue as IMatchable; IMatchable copyMatch = copyValue as IMatchable; if (originalMatch != null && copyMatch != null) { bool res = originalMatch.Matches(copyMatch, out List <string> ignoreMe); return(res); } string origString = originalValue as string; if (origString != null) { string mString = copyValue as string; if (mString == null) { return(false); } return(origString.Equals(mString)); } IEnumerable originalList = originalValue as IEnumerable; if (originalList != null) { IEnumerable copyList = copyValue as IEnumerable; if (copyList != null) { IEnumerator e = copyList.GetEnumerator(); e.MoveNext(); foreach (object originalItem in originalList) { if (Match(originalItem, e.Current) == false) { return(false); } e.MoveNext(); } } return(true); } if (originalValue == null && copyValue == null) { return(true); } return(originalValue != null && originalValue.Equals(copyValue)); }
/// <summary> /// This tests the public properties from the two objects. If any properties implement /// the IMatchable interface, and do not match, this returns false. If any public /// properties are value types, and they are not equal, then this returns false. /// </summary> /// <param name="self">This matchable item. </param> /// <param name="other">The other item to compare to this item.</param> /// <returns>Boolean, true if there is a match.</returns> public static bool Matches(this IMatchable self, IMatchable other) { List <string> ignoreMe; return(self.Matches(other, out ignoreMe)); }