zoukankan      html  css  js  c++  java
  • .NET MVC通过反射获取数据修改历史记录,并插入数据表中

      本文属于原创,转载时请标明出处!

           折磨了我一个晚上的问题,奈何对物理的反射印象太深了,整天去想着物理的反射、折射怎么解。感谢少将哥哥给我的指点,经过一个晚上对反射的恶补,最终搞定了。纪念一下。

      1.核心代码:

     1 private static void IsUpdate<T>(T old, T current, string id)
     2         {
     3             Model.PerFileHistory history = new Model.PerFileHistory();
     4             Model.Atrributes.ModifyFields atrr = null;
     5             Type type = typeof(T);
     6             PropertyInfo[] propertys = type.GetProperties();
     7             foreach (PropertyInfo property in propertys)
     8             {
     9                 if (property.PropertyType.IsValueType || property.PropertyType.Name == "String")
    10                 {
    11                     if (property.PropertyType.FullName.Contains("Guid"))
    12                         continue;
    13                     //if (property.Name != "CreateUserID" && property.Name != "CreateTime" && property.Name != "ModifyUserID" &&                                    property.Name != "LastModifyTime")//排除这些字段不做判断
    14                     //{
    15                     if (property.GetCustomAttributes(typeof(Model.Atrributes.ModifyFields), false).Count() > 0)
    16                     {
    17                         object o1 = property.GetValue(old, null); //以前的值
    18                         object o2 = property.GetValue(current, null); //修改后的值
    19                         string str1 = o1 == null ? string.Empty : o1.ToString();
    20                         string str2 = o2 == null ? string.Empty : o2.ToString();
    21                         //判断两者是否相同,不同则插入历史表中
    22                         if (str1 != str2)
    23                         {
    24                             history.BeforeValue = str1; //修改前的值
    25                             history.AfterValue = str2; //修改后的值
    26                             history.PCardNo = id; //修改数据的ID
    27                             history.IPAddress = HanNeng.Common.GetClientIP.GetRealIP(); //获取当前用户的IP地址
    28                             atrr = property.GetCustomAttributes(typeof(Model.Atrributes.ModifyFields), false)[0] as                    Model.Atrributes.ModifyFields;
    29                             history.ModifyField = property.Name;  //修改的字段名称
    30                             history.ModifyFieldName = atrr.FieldsName; //修改的字段中文名称
    31 
    32                             new BLL.PerFileHistory().AddModel(history);
    33                         }
    34                     }
    35                     //}
    36                 }
    37             }
    38         }
    View Code

      2.获取字段中文名,这个是在Model的类名里设置,示例如下:

     1 /// <summary>
     2     /// 获取字段名称
     3     /// </summary>
     4     public class ModifyFields : Attribute
     5     {
     6          public ModifyFields()
     7         {
     8         }
     9          public ModifyFields(string name)
    10         {
    11             this.FieldsName = name;
    12         }
    13         /// <summary>
    14         /// 修改的字段中文名
    15         /// </summary>
    16          public string FieldsName
    17         {
    18             get;
    19             set;
    20         }
    21     }
    Model
    1          /// <summary>
    2         /// 科部
    3         /// </summary>
    4         [Atrributes.ModifyFields("科部")]
    5         public int? SubjectDep
    6         {
    7             set { _subjectdep = value; }
    8             get { return _subjectdep; }
    9         }
    Model类名示例

      3.调用方式示例:

     1          if (id != null)
     2                 {
     3                     Model.PersonFile Person = bllPerson.GetModel<Model.PersonFile>(id);
     4                     if (modelPerson != null)
     5                     {
     6                         Model.Identity identity = Session["Identity"] as Model.Identity;
     7                         //if (identity.RoleIDs.ToString() == "R01")  //如果是系统管理员,则不记录历史
     8                         //{
     9                         //对前后数据的不同进行比较
    10                         Model.PersonFile OldPerson = Person;
    11                         Model.PersonFile NewPerson = modelPerson;
    12                         NewPerson.PersonAutoID = OldPerson.PersonAutoID;
    13                         IsUpdate(OldPerson, NewPerson, id);
    14                         //}
    15                     }
    16                 }
    Controller.CS

      4.最终的效果图:

  • 相关阅读:
    js-监听网络状态
    call、apply、bind三者比较
    弹框滑动击穿问题
    Vue指令及自定义指令的使用
    vue-cli 运行打开浏览器
    递归判断多维数组中对象是否有值
    sync 修饰符在Vue中如何使用
    自定义组件 v-model 的使用
    Object.keys( )与 for in 区别
    mongodb 安装
  • 原文地址:https://www.cnblogs.com/jingshuisihan/p/3850978.html
Copyright © 2011-2022 走看看