private static DeltaObject GetDeltaObject <T>(DeltaProperty deltaProperty, T originalObject, T newObject, List <PropertyInfo> propertiesToIgnoreOnDefault, bool ignorePropertiesOnDefault) { var propertyInfo = deltaProperty.PropertyInfo; var newValue = propertyInfo.GetValue(newObject); if (propertyInfo.IgnoreDeltaBecauseDefault(propertiesToIgnoreOnDefault, newValue, ignorePropertiesOnDefault)) { return(null); } var originalValue = deltaProperty.PropertyInfo.GetValue(originalObject); if (deltaProperty.HasDelta(originalValue, newValue)) { return(new DeltaObject { ValueConversionStatus = ValueConversionStatus.Success, PropertyName = propertyInfo.Name, PropertyAlias = deltaProperty.Alias, OriginalValue = originalValue, NewValue = newValue, StringifiedOriginalValue = originalValue?.ToString(), StringifiedNewValue = newValue?.ToString() }); } return(null); }
public static bool HasDelta(this DeltaProperty deltaProperty, object originalValue, object newValue) { var comparableOriginalValue = originalValue as IComparable; var comparableNewValue = newValue as IComparable; if (comparableOriginalValue == null && comparableNewValue == null) { return(false); } if (comparableOriginalValue == null && comparableNewValue != null) { return(true); } if (comparableOriginalValue != null && comparableNewValue == null) { return(true); } return(comparableOriginalValue.CompareTo(comparableNewValue) != 0); }
private static DeltaObject GetDeltaObject <T>(DeltaProperty deltaProperty, T originalObject, JObject jObject, List <PropertyInfo> propertiesToIgnoreOnDefault, bool ignorePropertiesOnDefault) { var propertyInfo = deltaProperty.PropertyInfo; if (!jObject.TryGetValue(propertyInfo.Name, #if NETSTANDARD1_6 StringComparison.CurrentCultureIgnoreCase, #else StringComparison.InvariantCultureIgnoreCase, #endif out var newValue)) { return(null); } var originalValue = deltaProperty.PropertyInfo.GetValue(originalObject); var typeConverter = TypeCache.GetTypeConverter(propertyInfo.PropertyType); var stringifiedNewValue = newValue.Type != JTokenType.Null ? newValue.ToString() : null; object convertedNewValue; try { convertedNewValue = stringifiedNewValue != null? typeConverter.ConvertFromString(stringifiedNewValue) : null; } catch (Exception) { return(new DeltaObject { ValueConversionStatus = ValueConversionStatus.Fail, PropertyName = propertyInfo.Name, PropertyAlias = deltaProperty.Alias, OriginalValue = originalValue, NewValue = stringifiedNewValue, StringifiedOriginalValue = originalValue?.ToString(), StringifiedNewValue = stringifiedNewValue }); } if (propertyInfo.IgnoreDeltaBecauseDefault(propertiesToIgnoreOnDefault, convertedNewValue, ignorePropertiesOnDefault)) { return(null); } if (deltaProperty.HasDelta(originalValue, convertedNewValue)) { return(new DeltaObject { ValueConversionStatus = ValueConversionStatus.Success, PropertyName = propertyInfo.Name, PropertyAlias = deltaProperty.Alias, OriginalValue = originalValue, NewValue = convertedNewValue, StringifiedOriginalValue = originalValue?.ToString(), StringifiedNewValue = convertedNewValue?.ToString() }); } return(null); }