C#对象深度克隆
有基础的开发者都应该很明白,对象是一个引用类型,例如: object b=new object(); object a=b; 那么a指向的是b的地址,这样在有些时候就会造成如果修改a的值,那么b的值也会跟随着改变(a和b是同一个引用内存地址)。 我们想要a和b都是各自互不影响的,那么只能是完全地新建一个新的对象,并且把现有对象的每个属性的值赋给新的对象的属性。也就是值类型的复制,这个操作就叫深度克隆。 这里我们写两个泛型方法分别对对象T和集合List<T>进行深度克隆的实现,我们的方法实现方式是“扩展方法”,就是能在原有的对象后面直接“点”操作。 下面我们来看一下深度克隆的算法实现: usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Reflection;/** *author:qixiao *create:2017-5-2511:52:21 **/namespaceQX_Frame.Helper_DG.Extends {publicstaticclassCloneExtends {publicstaticTDeepCloneObject<T>(thisTt)whereT:class { Tmodel=System.Activator.CreateInstance<T>();//实例化一个T类型对象 PropertyInfo[]propertyInfos=model.GetType().GetProperties();//获取T对象的所有公共属性 foreach(PropertyInfopropertyInfoinpropertyInfos) {//判断值是否为空,如果空赋值为null见else if(propertyInfo.PropertyType.IsGenericType&&propertyInfo.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) {//如果convertsionType为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换 NullableConverternullableConverter=newNullableConverter(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); } }returnmodel; }publicstaticIList<T>DeepCloneList<T>(thisIList<T>tList)whereT:class { IList<T>listNew=newList<T>();foreach(varitemintList) { Tmodel=System.Activator.CreateInstance<T>();//实例化一个T类型对象 PropertyInfo[]propertyInfos=model.GetType().GetProperties();//获取T对象的所有公共属性 foreach(PropertyInfopropertyInfoinpropertyInfos) {//判断值是否为空,如果空赋值为null见else if(propertyInfo.PropertyType.IsGenericType&&propertyInfo.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) {//如果convertsionType为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换 NullableConverternullableConverter=newNullableConverter(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); }returnlistNew; } } } 本文转自 sshpp 51CTO博客,原文链接:http://blog.51cto.com/12902932/1949344,如需转载请自行联系原作者