Copy the value of the property between the same two classes of example C #

       When an entity performing switching operation typically requires between two entities in two identical class attribute field to be a mutual conversion, we want a target value are copied to all fields b objects, we can only use b. property = a. attribute to write, if the attribute field too much, then we would write a lot of lines to copy statement, and also easy to miss some, this can be used when c # reflection to achieve replication.

1. When applied to create an entity from an entity as a data source assignment

        ///  <Summary> 
        /// reflector replicated to achieve the same attribute values of the object between the two classes
         /// applies to the initialization of the new entity
         ///  </ Summary> 
        ///  <typeParam name = "D"> Back entity </ typeParam> 
        ///  <typeParam name = "S"> data source entity </ typeParam> 
        ///  <param name = "S"> data source entity </ param> 
        ///  <returns> Back the new entity </ Returns> 
        public  static D Mapper <D, S> (S S)
        {
            D D = the Activator.CreateInstance <D> (); // Construct a new instance 
            try
            {
                var Types s.GetType = (); // get the type   
                var the Typed = typeof (D);
                 the foreach (SP PropertyInfo in Types.GetProperties ()) // get the type attribute field of   
                {
                     the foreach (DP PropertyInfo in Typed.GetProperties () )
                    {
                        IF (dp.Name == == sp.PropertyType dp.PropertyType sp.Name && && dp.Name! = " Error " && dp.Name! = " Item " ) // determines whether or not the same name attribute   
                        {
                            dp.SetValue (d, sp.GetValue (s, null ), null ); // value s obtained copy object properties to the properties of the object d   
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return d;
        }        

 

2. For not created entity, converting data between two entities

        ///  <Summary> 
        /// reflector replicated values of the same attribute class between two objects implemented
         /// applicable between no new entities
         ///  </ Summary> 
        ///  <typeParam name = "D "> return entity </ typeParam> 
        ///  <typeParam name =" S "> data source entity </ typeParam> 
        ///  <param name =" D "> entity returns the </ param> 
        ///  <param name = "s"> data source entity </ param> 
        ///  <Returns> </ Returns> 
        public  static D MapperToModel <D, S> (D D, S S)
        {
            try
            {
                var Types s.GetType = (); // get the type   
                var the Typed = typeof (D);
                 the foreach (SP PropertyInfo in Types.GetProperties ()) // get the type attribute field of   
                {
                     the foreach (DP PropertyInfo in Typed.GetProperties () )
                    {
                        IF (dp.Name == == sp.PropertyType dp.PropertyType sp.Name && && dp.Name! = " Error " && dp.Name! = " Item " ) // determines whether or not the same name attribute   
                        {
                            dp.SetValue (d, sp.GetValue (s, null ), null ); // value s obtained copy object properties to the properties of the object d   
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return d;
        }        

 

 

 

 

 

*****************************************************
*** No matter how far you go, looking back is also necessary. ***
*****************************************************

Guess you like

Origin www.cnblogs.com/gangle/p/11905331.html