. Net mvc obtains the data modification history through reflection and inserts it into the data table.
After suffering a night's problem, I had a deep impression on physical reflection. I thought about how to solve physical reflection and refraction all day. I would like to thank my brother-in-law for giving me some advice. After one night of preparation for reflection, I finally got it done. To commemorate it. 1. core code: Copy code 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 {1 1 if (property. PropertyType. FullName. Contains ("Guid") 12 continue; 13 // if (property. Name! = "CreateUserID" & property. Name! = "CreateTime" & property. Name! = "ModifyUserID" & property. Name! = "LastModifyTime") // exclude these fields. 14 // {15 if (property. getCustomAttributes (typeof (Model. atrributes. modifyFields), false ). count ()> 0) 16 {17 object o1 = property. getValue (old, null); // previous value 18 object o2 = property. getValue (current, null); // The modified value 19 string str1 = o1 = null? String. Empty: o1.ToString (); 20 string str2 = o2 = null? String. Empty: o2.ToString (); 21 // determine whether the two are the same. if different, insert the 22 if (str1! = Str2) 23 {24 history. beforeValue = str1; // The value before modification is 25 history. afterValue = str2; // The modified value 26 history. PCardNo = id; // modify the ID27 history of the data. IPAddress = HanNeng. common. getClientIP. getRealIP (); // get the IP address of the current user 28 atrr = property. getCustomAttributes (typeof (Model. atrributes. modifyFields), false) [0] as Model. atrributes. modifyFields; 29 history. modifyField = property. name; // modified field Name 30 history. modifyFieldName = atrr. F IeldsName; // The Chinese name of the modified field 31 32 new BLL. perFileHistory (). addModel (history); 33} 34} 35 //} 36} 37} 38} copy Code 2. obtain the Chinese name of the field, which is set in the Class Name of the Model. The example is as follows: copy code 1 // <summary> 2 // obtain the field name 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 // The Chinese name of the modified field 15 /// </summary> 16 public string Fields Name17 {18 get; 19 set; 20} 21} copy code 1 // <summary> 2 // department 3 /// </summary> 4 [Atrributes. modifyFields ("Division")] 5 public int? SubjectDep6 {7 set {_ subjectdep = value;} 8 get {return _ subjectdep;} 9} copy code 3. Call method example: Copy code 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") // if it is a system administrator, the system administrator does not record the historical data. 8 // {9 // compare the data before and after 10 models. personFile OldPerson = Person; 11 Model. personFile NewPerson = modelPerson; 12 NewPerson. personAutoID = OldPerson. personAutoID; 13 IsUpdate (OldPerson, NewPerson, id); 14 //} 15} 16}