C # Object Deep cloning

Source: Internet
Author: User

A basic developer should be aware that an object is a reference type, for example:

Object B=new object ();

Object a=b;

Then a points to the address of B, which in some cases causes if the value of a is modified, then the value of B will follow the change (A and B are the same reference memory address).

We want both A and B to be independent, so we can only create a new object completely, and assign the value of each property of an existing object to the properties of the new object. That is, the value type of replication, this operation is called deep cloning.

Here we write two generic methods for the implementation of the deep cloning of the object T and the set list<t>, our method is implemented in the way of "extension method", that is, can be directly behind the original object "point" operation.

Let's take a look at the algorithm implementation of deep cloning:

Using system;using system.collections.generic;using system.componentmodel;using system.reflection;/** * Author: Qixiao * create:2017-5-25 11:52:21 * */namespace QX_FRAME.HELPER_DG. extends{public static class Cloneextends {public static T deepcloneobject<t> (this T t) where T:clas                     s {T model = system.activator.createinstance<t> (); Instantiate a type of T object propertyinfo[] Propertyinfos = model. GetType ().     GetProperties (); Gets all public properties of the T object foreach (PropertyInfo PropertyInfo in Propertyinfos) {//judgment value is null if NULL assignment null See else if (PropertyInfo.PropertyType.IsGenericType && PropertyInfo.PropertyType.GetGenericTypeD Efinition (). Equals (typeof (Nullable<>))) {//If Convertsiontype is a Nullable class, declare a Nullableconverter class , the class provides conversions from the nullable class to the underlying primitive type nullableconverter nullableconverter = new Nullableconverter (propertyinfo.prop Ertytype);                   Convert Convertsiontype to the underlying primitive type of the nullable pair Propertyinfo.setvalue (model, Convert.changetyp                E (Propertyinfo.getvalue (t), nullableconverter.underlyingtype), NULL); } else {Propertyinfo.setvalue (model, Convert.changetype (propertyinfo.get                Value (t), propertyinfo.propertytype), NULL);        }} return model;            } public static ilist<t> deepclonelist<t> (this ilist<t> tList) where T:class {            ilist<t> listnew = new list<t> ();                     foreach (var item in tList) {T model = system.activator.createinstance<t> (); Instantiate a type of T object propertyinfo[] Propertyinfos = model. GetType ().     GetProperties (); Gets all public properties of the T object foreach (PropertyInfo PropertyInfo in Propertyinfos) {//sentenced             If the null assignment is NULL if the value is null see else       if (PropertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition (). Equals (typeof (Nullable<>))) {//If Convertsiontype is a Nullable class, declare a Nullableco The Nverter class, which provides conversions from the nullable class to the underlying primitive type nullableconverter nullableconverter = new Nullableconverter (prope                        Rtyinfo.propertytype); Convert Convertsiontype to the base primitive type of the nullable pair Propertyinfo.setvalue (model, Convert.changetype (propertyInfo.                    GetValue (item), nullableconverter.underlyingtype), NULL); } else {Propertyinfo.setvalue (model, Convert.changetype (prop                    Ertyinfo.getvalue (item), propertyinfo.propertytype), NULL);            }} listnew.add (model);        } return listnew; }    }}

The above code has implemented the deep cloning operation, in use We are as follows:

For example, with the user class, we can do this

User User1=new user ();

User User2=user1. Deepcloneobject ();

This completes the deep cloning of the User1!

C # Object Deep cloning

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.