using System.Collections; using System.Reflection; namespace Connected.Interop.Reflection { public static class ObjectComparer { public static bool Compare(object left, object right) { if (left is null || right is null) return false; if (left.GetType() != right.GetType()) return false; var leftProperties = left.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); ; foreach (var property in leftProperties) { if (!Compare(left, right, property)) return false; } return true; } private static bool Compare(object left, object right, PropertyInfo property) { if (property.PropertyType.IsEnumerable()) { if (!Compare(left as IEnumerable, right as IEnumerable)) return false; } else if (property.PropertyType.IsTypePrimitive()) { if (Comparer.DefaultInvariant.Compare(left, right) != 0) return false; } else { var leftValue = property.GetValue(left); var rightValue = property.GetValue(right); if (!Compare(leftValue, rightValue)) return false; } return true; } private static bool Compare(IEnumerable left, IEnumerable right) { var leftEn = left.GetEnumerator(); var rightEn = right.GetEnumerator(); while (leftEn.MoveNext()) { if (!rightEn.MoveNext()) return false; if (!Compare(leftEn.Current, rightEn.Current)) return false; } if (rightEn.MoveNext()) return false; return true; } } }