直接上代码,传入新旧两个Model类
字段说明,要加display标签:
代码如下:
public static class EntityExtension
{
public static HashSet<Type> PrimitiveTypes = null;
static EntityExtension()
{
PrimitiveTypes = new HashSet<Type>()
{
typeof(String),
typeof(Byte[]),
typeof(Byte),
typeof(Int16),
typeof(Int32),
typeof(Int64),
typeof(Single),
typeof(Double),
typeof(Decimal),
typeof(DateTime),
typeof(Guid),
typeof(Boolean),
typeof(TimeSpan),
typeof(Byte?),
typeof(Int16?),
typeof(Int32?),
typeof(Int64?),
typeof(Single?),
typeof(Double?),
typeof(Decimal?),
typeof(DateTime?),
typeof(Guid?),
typeof(Boolean?),
typeof(TimeSpan?)
};
}
public static string GetChangedFields<T>(this T newEntity, T oldEntity) where T : class
{
StringBuilder updatedFields = new StringBuilder();
Type entityType = typeof(T);
PropertyInfo[] properties = entityType.GetProperties().Where(o => o.CanWrite && PrimitiveTypes.Contains(o.PropertyType) && !o.GetCustomAttributes(false).OfType<NotMappedAttribute>().Any()).ToArray();
foreach (var p in properties)
{
if (p.Name == "ModifiedDate" || p.Name == "ModifiedByName" || p.Name == "ModifiedById") continue;
object oldValue = p.GetValue(oldEntity, null);
object newValue = p.GetValue(newEntity, null);
if ((oldValue == null && newValue == null))
{
continue;
}
else if (oldValue == null && newValue != null || oldValue != null && newValue == null || !Eq(p.PropertyType, oldValue, newValue))
{
string fieldName;
var display = p.GetCustomAttribute<DisplayAttribute>(false);
fieldName = display != null ? display.Name : p.Name;
updatedFields.AppendFormat("{0}:{1}->{2}; ", fieldName, oldValue ?? "NULL", newValue ?? "NULL");
}
}
return updatedFields.ToString();
}
private static bool Eq(Type propertyType, object oldValue, object newValue)
{
if (propertyType == typeof(Decimal) || propertyType == typeof(Decimal?))
{
return decimal.Parse(oldValue.ToString()) == decimal.Parse(newValue.ToString());
}
else
{
return string.Equals(oldValue.ToString(), newValue.ToString());
}
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="newEntity"></param>
/// <param name="oldEntity"></param>
/// <returns></returns>
public static List<ValueModifiedModel> GetChangedFieldsExtent<T>(this T newEntity, T oldEntity) where T : class
{
List<ValueModifiedModel> valueModifiedList = new List<ValueModifiedModel>();
Type entityType = typeof(T);
PropertyInfo[] properties = entityType.GetProperties().Where(o => o.CanWrite && PrimitiveTypes.Contains(o.PropertyType) && !o.GetCustomAttributes(false).OfType<NotMappedAttribute>().Any()).ToArray();
foreach (var p in properties)
{
if (ignoreColumnList.Contains(p.Name))
continue;
object oldValue = p.GetValue(oldEntity, null);
object newValue = p.GetValue(newEntity, null);
if ((oldValue == null && newValue == null))
{
continue;
}
else if (oldValue == null && newValue != null || oldValue != null && newValue == null || !Eq(p.PropertyType, oldValue, newValue))
{
valueModifiedList.Add(new ValueModifiedModel()
{
ColumnName = p.Name,
ColumnChineseName = p.GetPropertyDisplayName(),
ChangeBefore = oldValue ?? "NULL",
ChangeAfter = newValue ?? "NULL"
}
);
}
}
return valueModifiedList;
}
/// <summary>
/// /
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="pTargetObjSrc"></param>
/// <param name="pTargetObjDest"></param>
public static void EntityToEntity<T>(T pTargetObjSrc, T pTargetObjDest) where T : class
{
try
{
foreach (var mItem in typeof(T).GetProperties())
{
mItem.SetValue(pTargetObjDest, mItem.GetValue(pTargetObjSrc, new object[] { }), null);
}
}
catch (NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception Ex)
{
throw Ex;
}
}
/// <summary>
/// 忽略的列名列表
/// </summary>
public static List<string> ignoreColumnList = new List<string>()
{
};
}