C# deep clone generic object T and extension method of List<T>

Basic developers should understand that an object is a reference type, for example:

object b=new object();

object a=b;

Then a points to the address of b, so that sometimes if the value of a is modified, the value of b will also change accordingly (a and b are the same reference memory address).

We want a and b to be independent of each other, so we can only create a new object completely, and assign the value of each attribute of the existing object to the attribute of the new object. That is, the copying of value types, this operation is called deep cloning.

Here we write two generic methods to implement deep cloning of the object T and the collection List respectively. Our method implementation method is an "extension method", which means that we can directly "click" operations behind the original object.

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 : class
        {
    
    
            T model = System.Activator.CreateInstance<T>();                     //实例化一个T类型对象
            PropertyInfo[] propertyInfos = model.GetType().GetProperties();     //获取T对象的所有公共属性
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
    
    
                //判断值是否为空,如果空赋值为null见else
                if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                {
    
    
                    //如果convertsionType为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换
                    NullableConverter nullableConverter = new NullableConverter(propertyInfo.PropertyType);
                    //将convertsionType转换为nullable对的基础基元类型
                    propertyInfo.SetValue(model, Convert.ChangeType(propertyInfo.GetValue(t), nullableConverter.UnderlyingType), null);
                }
                else
                {
    
    
                    propertyInfo.SetValue(model, Convert.ChangeType(propertyInfo.GetValue(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>();                     //实例化一个T类型对象
                PropertyInfo[] propertyInfos = model.GetType().GetProperties();     //获取T对象的所有公共属性
                foreach (PropertyInfo propertyInfo in propertyInfos)
                {
    
    
                    //判断值是否为空,如果空赋值为null见else
                    if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                    {
    
    
                        //如果convertsionType为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换
                        NullableConverter nullableConverter = new NullableConverter(propertyInfo.PropertyType);
                        //将convertsionType转换为nullable对的基础基元类型
                        propertyInfo.SetValue(model, Convert.ChangeType(propertyInfo.GetValue(item), nullableConverter.UnderlyingType), null);
                    }
                    else
                    {
    
    
                        propertyInfo.SetValue(model, Convert.ChangeType(propertyInfo.GetValue(item), propertyInfo.PropertyType), null);
                    }
                }
                listNew.Add(model);
            }
            return listNew;
        }
    }
}

The above code has realized the operation of deep cloning, and we use it as follows:

For example, if there is a User class, we can do this

User user1=new User();

User user2=user1.DeepCloneObject();

This completes the deep clone of user1!

Source: https://www.cnblogs.com/7tiny/p/7007291.html

Guess you like

Origin blog.csdn.net/weixin_45499836/article/details/126261373