using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespace sharpcomparer { public class PropertyComparison : Comparison { public string Name { get { return LeftItem != null ? LeftItem.Name : RightItem.Name; } } public string Value { get { return LeftItem != null ? LeftItem.Value : RightItem.Value; } } public PropertyComparison(Property leftItem, Property rightItem, PropertyComparison.ComparisonDifference difference) : base(leftItem, rightItem, difference) { } public static List Compare(T left, T right) { List comparison = new List(); PropertyInfo[] infos = typeof(T).GetProperties(); foreach (PropertyInfo info in infos) { object[] attributes = info.GetCustomAttributes(typeof(Hide), true); if (attributes.Length == 0) { string name = info.Name; string leftValue = left != null ? info.GetValue(left, null).ToString() : null; string rightValue = right != null ? info.GetValue(right, null).ToString() : null; Property leftProperty = left == null ? null : new Property(name, leftValue); Property rightProperty = right == null ? null : new Property(name, rightValue); Comparison.ComparisonDifference difference = PropertyComparison.ComparisonDifference.None; if (left == null) difference = PropertyComparison.ComparisonDifference.RightOnly; else if (right == null) difference = PropertyComparison.ComparisonDifference.LeftOnly; else if (leftValue != rightValue) difference = PropertyComparison.ComparisonDifference.Different; comparison.Add(new PropertyComparison(leftProperty, rightProperty, difference)); } } return comparison; } } }