C#对象的深度复制在此应用程序中,我定义了一个自定义集合,如下所示:publicclassResultList:IEnumerable{publicListResults{get;放;}publicdecimalCenterLatitude{get;放;}publicdecimalCenterLongitude{get;放;}}结果使用的类型是三种自定义类型之一。每个自定义类型的属性只是原始类型(整数、字符串、布尔值、int?、bool?)。下面是其中一种自定义类型的示例:publicclassResultItem{publicintID{get;放;}公共字符串名称{得到;放;公共布尔?isLegit{得到;对象的深拷贝。我找到了这篇文章:Genericwaytocreateadeepcopyofallelementsinacollection。但是,我不知道该怎么做。您的ResultList类不能与JonSkeet的示例一起工作的原因之一是因为它没有实现ICloneable接口。在所有需要克隆的类上实现ICloneable,例如publicclassResultItem:ICloneable{publicobjectClone(){varitem=newResultItem{ID=ID,Name=Name,isLegit=isLegit};归还物品;}}andResultList:publicclassResultList:IEnumerable,ICloneablewhereT:ICloneable{publicListResults{get;放;}publicdecimalCenterLatitude{get;放;}publicdecimalCenterLongitude{get;放;}publicobjectClone(){varlist=newResultList{CenterLatitude=CenterLatitude,CenterLongitude=CenterLongitude,Results=Results.Select(x=>x.Clone()).Cast().ToList()};返回列表;然后对对象进行深拷贝:resultList。克隆();编码涉及最少的方法是通过BinaryFormatter进行序列化和反序列化。您可以定义以下扩展方法(取自Kilhoffer的回答):publicstaticTDeepClone(Tobj){using(varms=newMemoryStream()){formatter.Serialize(ms,obj);ms.Position=0;返回(T)formatter.Deserialize(ms);}}...然后调用:ResultListclone=DeepClone(original);扩展@Georgi-it,我不得不修改他的代码以处理类型为inheritanceList的其他属性:isT)){thrownewException("克隆对象必须匹配输出类型");}return(T)Clone(obj,deep);}publicstaticobjectClone(objectobj,booldeep){if(obj==null){returnnull;类型objType=obj.GetType();if(objType.IsPrimitive||objType==typeof(string)||objType.GetConstructors().FirstOrDefault(x=>x.GetParameters().Length==0)==null){returnobj;}列表属性=objType.GetProperties().ToList();如果(深){properties.AddRange(objType.GetProperties(BindingFlags.实例|BindingFlags.NonPublic));}objectnewObj=Activator.CreateInstance(objType);foreach(varpropinproperties){if(prop.GetSetMethod()!=null){varproceed=true;if(objisIList){varlistType=obj.GetType().GetProperty("Item").PropertyType;如果(prop.PropertyType==listType){proceed=false;foreach(variteminobjasIList){objectclone=Clone(item,deep);(newObj作为IList).Add(克隆);}}}if(proceed){objectpropValue=prop.GetValue(obj,null);对象克隆=克隆(propValue,深);prop.SetValue(newObj,clone,null);}}}返回newObj;}}下面是我需要写的,里面使用反射复制每个属性(或者privateifspecified)deepcopy共享的所有内容,如果对你有用,需要了解更多C#学习教程,希望你多加注意——publicstaticclassObjectCloner{publicstaticTClone(objectobj,booldeep=false)whereT:new(){if(!(objisT)){thrownewException("Cloningobjectmustmatchoutputtype");}return(T)Clone(obj,deep);}publicstaticobjectClone(objectobj,booldeep){if(obj==null){返回null;类型objType=obj.GetType();if(objType.IsPrimitive||objType==typeof(string)||objType.GetConstructors().FirstOrDefault(x=>x.GetParameters().Length==0)==null){returnobj;}列表属性=objType.GetProperties().ToList();如果(深){properties.AddRange(objType.GetProperties(BindingFlags.Instance|BindingFlags.NonPublic));}objectnewObj=Activator.CreateInstance(objType);foreach(varpropinproperties){if(prop.GetSetMethod()!=null){objectpropValue=prop.GetValue(obj,null);对象克隆=克隆(propValue,深);prop.SetValue(newObj,clone,null);}}返回新对象;}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
