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

Posted 真爱无限

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.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>()
        

        ;
    

以上是关于.Net中自动生成Model字段修改日志内容的主要内容,如果未能解决你的问题,请参考以下文章

sql server新旧数据库的表结构差异

一个diff工具,用于判断两个目录下所有的改动(比较新旧版本文件夹)

django 模型——model 字段

Django model 中的字段解释

MySql生成.Net中Model类的存储过程

kettle合并记录 新旧表明明关键字段id一致结果老数据标记删除,新数据标记插入,求解