attribute assignment for C # collection objects

Source: Internet
Author: User

(a) Foreword

After the mutual assignment between object attributes, the assignment of attributes of a collection object can be performed by iterating through the objects in the collection. These can be applied to situations where different objects and related properties are similar. The most common is the assignment of objects between Web services and Silverlight (where only part of the property value between objects is needed), which reduces Silverlight's reliance on Web services.

(ii) Concrete implementation

Assigns a value to a property between the source object and the target object through reflection. The source object's property name, property type must be the same as the target object's property name, property type, and the property of the source object must be readable, and the target object's properties are writable (only for properties that need to be assigned). The specific source code is as follows:

public class Objectmapper {private static readonly dictionary<string, Ilist<propertymapper>> _
    
        Mappers = new dictionary<string, ilist<propertymapper>> ();
            public static ilist<propertymapper> getmapperproperties (type sourcetype, type TargetType) {
            var sourceproperties = sourcetype.getproperties ();
    
            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 (); public static void Copyproperties<t1, T2> (list<t1> sources, list<t2> targets) where t2:new () {if (sources = null | | sources. Count = 0 | |
            Targets = = null) {return;
            } T2 Target;
                foreach (T1 source in sources) {target = new T2 ();
                Copyproperties (source, target); Targets.
            ADD (target); The public static void Copyproperties (object source, object target) {if (source = = NULL | |
            target = = null) {return; var sourcetype = source.
            GetType (); var targettype = target.
            GetType ();
            String key = Getmapperkey (SourceType, targettype); if (!_mappers.
            ContainsKey (key)) {mapperproperties (sourcetype, targettype);
    
            var mapperproperties = _mappers[key]; Setpropertityvalue (source, Target, mapperproperties); private static void Setpropertityvalue (object source, object target, ILIST&LT;PROPERTYMAPPER&G T
            Mapperproperties) {for (int index = 0, count = mapperproperties.count; index < count; index++)
                {var property = Mapperproperties[index]; var Sourcevalue = property.
                Sourceproperty.getvalue (source, NULL); Property.
            Targetproperty.setvalue (target, sourcevalue, NULL); } protected static string Getmapperkey (type sourcetype, type TargetType) {Retu RN String.
        Format ("{0}_{1}", Sourcetype.fullname, Targettype.fullname); public static void Mapperproperties (type source, type target) {if Source = null | |
            Arget = = null) {return;
            String key = Getmapperkey (source, target); if (_mapPers.
            ContainsKey (key)) {return;
            var properties = getmapperproperties (source, target); _mappers.
        ADD (key, properties); }
    }

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.