Mutual assignment between object properties

Source: Internet
Author: User

(a) Foreword

When the two classes under different namespaces have the same attributes and need to be assigned to each other, the Jasen.Core.Info class in the following figure and the instance of the Jasen.Core.Test.Info class need to be assigned to each other, and the value is directly assigned according to the general idea. Typically, this situation is more common when invoking Web service. When you need to convert a lot of classes, or need to convert a lot of properties, we need to be based on certain rules to the design of this scenario, who will not be silly Bragi an attribute to the object to assign value.

(ii) The Objectmapper class is responsible for assigning values between the corresponding attributes of the object

<summary>//////</summary> public class Objectmapper {///<summary> </summary>///<param name= "sourcetype" ></param>///<param N Ame= "TargetType" ></param>///<returns></returns> public static Ilist<propertymapp Er> getmapperproperties (Type sourcetype, type TargetType) {var sourceproperties = Sourcetype.getpr
            Operties ();
    
            var targetproperties = targettype.getproperties (); Return (from S in Sourceproperties from T in targetproperties where s.name = = T.na Me && s.canread && t.canwrite && s.propertytype = = T.propertytype Select New P
                                  Ropertymapper {sourceproperty = s,
                       TargetProperty = t       }).
        ToList (); }///<summary>//////</summary>///<param name= "source" ;</param>///<param name= "target" ></param> public static void Copyproperties (Object sou Rce, Object target) {var sourcetype = source.
            GetType (); var targettype = target.
            GetType ();
    
            var mapperproperties = getmapperproperties (sourcetype, targettype); for (int index = 0,count=mapperproperties.count, index < count; index++) {var property = m
                Apperproperties[index]; var Sourcevalue = property.
                Sourceproperty.getvalue (source, NULL); Property.
            Targetproperty.setvalue (target, sourcevalue, NULL); }
        }
    }

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.