.Net中自动生成Model字段修改日志内容

直接上代码,传入新旧两个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>()
        {

        };
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值