Perform a shortest copy through the attribute and perform a shortest copy on the attribute.

Source: Internet
Author: User

Perform a shortest copy through the attribute and perform a shortest copy on the attribute.

In the past two days, I have read a code to be handed over by a departing colleague. The method of copying a copy is quite good. I will record it here.

I. Method body

Is a static method CopyHelper, which contains the following three parts

1. Add an extension method to the PropertyInfo [] type to check whether fields of the same type and name exist.

/// <Summary> // PropertyInfo [] extension method, obtain attributes by name /// </summary> /// <param name = "P"> </param> /// <param name = "pro"> </param >/// <returns> </returns> private static PropertyInfo GetPropertyByName (this PropertyInfo [] P, propertyInfo pro) {if (P = null) throw new NullReferenceException ("the parameter PropertyInfo [] of the GetPropertyByName () method in the CopyHelper class is Null"); var item = P. toList (). find (x => x. name. equals (pro. name) & (pro. propertyType. isSubclassOf (x. propertyType) | pro. propertyType. fullName. equals (x. propertyType. fullName); return item ;}

2. The shortest object method, that is, the SetValue method is used to assign values if it is not empty and can be written based on the above method.

/// <Summary> /// shallow copy /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "vo"> original VO </param> /// <param name = "viewModel"> vo under VM </param> /// <returns> </returns> private static T O2M <T> (object vo, T viewModel) {// obtains the attribute set PropertyInfo [] VmPInfos = viewmodel. getType (). getProperties (); // obtain the attribute set of the original vo PropertyInfo [] VoPInfos = vo. getType (). getProperties (); for (int I = 0; I <VoPI Nfos. length; I ++) {// find the var pro = VmPInfos attribute corresponding to VO from the VM. getPropertyByName (VoPInfos [I]); // if it is not empty and can be written, assign if (pro! = Null & pro. canWrite) pro. setValue (viewModel, VoPInfos [I]. getValue (vo, null), null);} return viewModel;} private static T O2M <T> (object vo) where T: new () {if (vo = null) return default (T); T viewModel = new T (); return O2M <T> (vo, viewModel );}

3. expose an extended Copy method to the outside and run the call

/// <Summary> /// assign values for attributes with the same field name and type (used for conversion between ViewModel and Vo) /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "vo"> </param> /// <returns> </returns> // <remarks> note that the value is a shortest copy, the Model will re-create new () </remarks> public static T CopyTo <T> (this object vo) where T: new () {return O2M <T> (vo );} /// <summary> /// assign values for attributes with the same field name and type (used for conversion between ViewModel and Vo) /// </summary> /// <typeparam name = "T"> </typeparam> /// <Param name = "vo"> </param> /// <returns> </returns> /// <remarks> note that the value is a shortest copy, model will re-create new () </remarks> public static bool CopyTo <T> (this object vo, T model) where T: new () {if (model = null) model = new T (); return O2M <T> (vo, model )! = Null ;}

Ii. Test

1. Create two entity classes, HumanVo and StudentVo.

public class HumanVo    {        private string _name;        private int _age;        private bool _sex;        public string Name        {            get            {                return _name;            }            set            {                _name = value;            }        }        public int Age        {            get            {                return _age;            }            set            {                _age = value;            }        }        public bool Sex        {            get            {                return _sex;            }            set            {                _sex = value;            }        }    }
public class StudentVo    {        private string _name;        private int _age;        private bool _sex;        private string _class;        private string _school;        public string Name        {            get            {                return _name;            }            set            {                _name = value;            }        }        public int Age        {            get            {                return _age;            }            set            {                _age = value;            }        }        public bool Sex        {            get            {                return _sex;            }            set            {                _sex = value;            }        }        public string Class        {            get            {                return _class;            }            set            {                _class = value;            }        }        public string School        {            get            {                return _school;            }            set            {                _school = value;            }        }    }

2. assign a value to HumanVo

Private void AddInfo () {HumanVo. Name = "Li Lei"; HumanVo. Age = 16; HumanVo. Sex = true ;}

3. Call the CopyTo Method

HumanVo.CopyTo(StudentVo);

4. view results

Before calling a method, track the code and view the values of the two VO

After calling the method, view the value of StudentVO

Related Article

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.