It should now be used frequently to record the operation log, Modify and add a change that must involve the property values of two Entities.
Use reflection to record Changes.
Remember that the attribute fields in the class need to be labeled Description :
For example:
< Summary > /// last modified </summary> [Description ("last modified")] Public DateTime piupdatetime {get; set;}
The relevant code is attached directly:
public classOprateloghelper {/// <summary> ///gets a description of the change in value between two objects/// </summary> /// <typeparam name= "T" >T</typeparam> /// <param name= "obj1" >the object before the change</param> /// <param name= "obj2" >the object after the change</param> /// <param name= "isdes" >whether or not the [Description] tag is filtered out</param> /// <returns>string</returns> public Static stringgetobjcomparestring<t> (t obj1, T obj2,BOOLIsdes)whereT:New() { stringres =string. Empty; if(obj1 = =NULL|| Obj2 = =NULL) { returnres; } varProperties = fromPropertyinch typeof(T). GetProperties (bindingflags.instance |Bindingflags.public)Selectproperty ; stringObjVal1 =string. Empty; stringObjVal2 =string. Empty; foreach(varPropertyinchproperties) { varIngorecompare = Attribute.GetCustomAttribute (property,typeof(ingorecompareattribute)); if(ingorecompare! =NULL) { Continue; } objVal1= Property. GetValue (obj1,NULL) ==NULL?string. Empty:property. GetValue (obj1,NULL). ToString (); ObjVal2= Property. GetValue (obj2,NULL) ==NULL?string. Empty:property. GetValue (obj2,NULL). ToString (); stringDes =string. Empty; DescriptionAttribute DescriptionAttribute= ((descriptionattribute) Attribute.GetCustomAttribute (property,typeof(descriptionattribute))); if(descriptionattribute! =NULL) {des= ((descriptionattribute) Attribute.GetCustomAttribute (property,typeof(descriptionattribute)). Description;//Property Value } if(isdes && DescriptionAttribute = =NULL) { Continue; } if(!objval1.equals (objVal2)) {res+= (string. IsNullOrEmpty (des)? Property. Name:des) +":"+ ObjVal1 +" -"+ ObjVal2 +";"; } } returnres; } } /// <summary> ///when some features are added, the attribute is ignored in the entity diff comparison/// </summary>[attributeusage (attributetargets.property, AllowMultiple =false, inherited =false)] public classIngorecompareattribute:attribute { publicIngorecompareattribute () {Flag=true; } public BOOLFlag {Get;Set; } }
When used, call the Getobjcomparestring method directly.
. NET two object comparisons, throwing results of a different field