/// <summary> /// 对比两个实例的属性值,如果目标实例属性的值和源实例属性的值不相等,则将源实例属性的值赋给目标实体的属性 /// </summary> /// <typeparam name="T">泛型对象</typeparam> /// <param name="distObject">目标实例</param> /// <param name="sourceObject">源实例</param> /// <param name="modifiedRoot"></param> /// <param name="ignoreProperties">需要忽略的属性值(被忽略的属性将不会把源实例的属性数据赋给目标实例的属性)</param> /// <returns></returns> public static T CompareObject <T>(this T distObject, T sourceObject, out EntityModifiedTracker modifiedRoot, string[] ignoreProperties = null) { modifiedRoot = new EntityModifiedTracker(); var disType = distObject.GetType(); var disProps = disType.GetProperties(); var sourceType = sourceObject.GetType(); var sourceProps = sourceType.GetProperties(); foreach (var disProp in disProps) { if (ignoreProperties != null && ignoreProperties.Length > 0 && ignoreProperties.Contains(disProp.Name)) { continue; } if (sourceProps.All(x => x.Name != disProp.Name)) { continue; } var disValue = disProp.GetValue(distObject, null); var sourceValue = sourceType.GetProperty(disProp.Name).GetValue(sourceObject, null); if (disValue == null && sourceValue == null) { continue; } if (disValue == null) { disProp.SetValue(distObject, sourceValue, null); modifiedRoot.Add(new Modified { PropertyName = disProp.Name, OldValue = null, NewValue = sourceValue }); disProp.SetValue(distObject, sourceValue, null); continue; } if (!disValue.Equals(sourceValue)) { modifiedRoot.Add(new Modified { PropertyName = disProp.Name, OldValue = disValue, NewValue = sourceValue }); disProp.SetValue(distObject, sourceValue, null); } } return(distObject); }
/// <summary> /// 对比两个实例的属性值,如果目标实例属性的值和源实例属性的值不相等,则将源实例属性的值赋给目标实体的属性 /// </summary> /// <typeparam name="T">泛型对象</typeparam> /// <param name="distObject">目标实例[原始值]</param> /// <param name="sourceObject">源实例[修改后的值]</param> /// <param name="ignoreProperties">需要忽略的属性值(被忽略的属性将不会把源实例的属性数据赋给目标实例的属性)</param> /// <returns></returns> public static EntityModifiedTracker GetModifiedTracker <T>(this T distObject, T sourceObject, string[] ignoreProperties = null) { var mod = new EntityModifiedTracker(); var disType = distObject.GetType(); var disProps = disType.GetProperties(); var sourceType = sourceObject.GetType(); var sourceProps = sourceType.GetProperties(); foreach (var disProp in disProps) { var attr = (EntityTrackerAttribute)disProp.GetCustomAttribute(typeof(EntityTrackerAttribute), false); if (attr != null && attr.Ignore) { var ignore = attr.Ignore; continue; } if (ignoreProperties != null && ignoreProperties.Length > 0 && ignoreProperties.Contains(disProp.Name)) { continue; } if (sourceProps.All(x => x.Name != disProp.Name)) { continue; } var disValue = disProp.GetValue(distObject, null); var sourceValue = sourceType.GetProperty(disProp.Name).GetValue(sourceObject, null); if (disValue == null && sourceValue == null) { continue; } if (disValue.Equals(sourceValue)) { continue; } var c = new Modified { PropertyName = disProp.Name, OldValue = disValue, NewValue = sourceValue }; if (attr != null && !string.IsNullOrEmpty(attr.Label)) { c.Label = attr.Label; } mod.Add(c); } return(mod); }