//https://stackoverflow.com/questions/42809686/c-sharp-compare-two-objects-for-properties-with-different-values public static object Diff <T>(T Original, T compare) { if (Original != null && compare != null) { var type = typeof(T); var allProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); var allSimpleProperties = allProperties.Where(pi => pi.PropertyType.IsSimpleType()); var unequalProperties = ( from pi in allSimpleProperties let AValue = type.GetProperty(pi.Name).GetValue(Original, null) let BValue = type.GetProperty(pi.Name).GetValue(compare, null) where AValue != BValue && (AValue == null || !AValue.Equals(BValue)) select new DiffProperty { Name = pi.Name, Origin = AValue, Changed = BValue }).ToList(); return(unequalProperties); } else { throw new ArgumentNullException(); } }
public static List <string> GetChangedProperties <T>(object A, object B) { if (A != null && B != null) { var type = typeof(T); var allProperties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | BindingFlags.DeclaredOnly).Where(x => !Attribute.IsDefined(x, typeof(SkipCompareAttribute))); var allSimpleProperties = allProperties.Where(pi => pi.PropertyType.IsSimpleType()); var unequalProperties = from pi in allSimpleProperties let AValue = type.GetProperty(pi.Name).GetValue(A, null) let BValue = type.GetProperty(pi.Name).GetValue(B, null) where AValue != BValue && (AValue == null || !AValue.Equals(BValue)) select pi.Name; return(unequalProperties.ToList()); } else if (A != null && B == null) { return(new List <string>() { "" }); } else { throw new ArgumentNullException("You need to provide 2 non-null objects"); } }
public List <string> GetChangedProperties <T>(object a, object b) { if (a == null || b == null) { throw new ArgumentNullException("You need to provide 2 non-null objects"); } var type = typeof(T); var allProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); var allSimpleProperties = allProperties.Where(pi => pi.CanWrite && IsSimpleType(pi.PropertyType)); var unequalProperties = from pi in allSimpleProperties let AValue = type.GetProperty(pi.Name).GetValue(a, null) let BValue = type.GetProperty(pi.Name).GetValue(b, null) where AValue != BValue && (AValue == null || !AValue.Equals(BValue)) select pi.Name; return(unequalProperties.ToList()); }