. NET two object comparisons, throwing results of a different field

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.