.NET 记录实体类修改字段方法

#region Model字段修改记录
        /// <summary>
        /// 获取Entity 修改的字段值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newEntity"></param>
        /// <param name="oldEntity"></param>
        /// <param name="ignoreColumnList">忽略的列名列表</param>
        /// <returns></returns>
        public static List<ValueModifiedModel> GetModifiedModels<T>( T newEntity, T oldEntity, List<string> ignoreColumnList) 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 || !TypeConvert(p.PropertyType, oldValue, newValue))
                {
                    valueModifiedList.Add(new ValueModifiedModel()
                    {
                        ColumnName = p.Name,
                        OldValue = oldValue ?? "NULL",
                        NewValue = newValue ?? "NULL"
                    }
                    );
                }
            }
            return valueModifiedList;
        }
        /// <summary>
        /// 字段类型
        /// </summary>
        public static HashSet<Type> 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?)       
         };
        /// <summary>
        /// 数据类型转换
        /// </summary>
        /// <param name="propertyType"></param>
        /// <param name="oldValue"></param>
        /// <param name="newValue"></param>
        /// <returns></returns>
        private static bool TypeConvert(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>
        public class ValueModifiedModel
        {
            /// <summary>
            /// 字段名称
            /// </summary>
            public string ColumnName { get; set; }

            /// <summary>
            /// 旧值
            /// </summary>
            public object OldValue { get; set; }

            /// <summary>
            /// 新值
            /// </summary>
            public object NewValue { get; set; }
        }
        #endregion

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值