C#对象深度克隆
有基础的开发者都应该很明白,对象是一个引用类型,例如:
object b=new object();
object a=b;
那么a指向的是b的地址,这样在有些时候就会造成如果修改a的值,那么b的值也会跟随着改变(a和b是同一个引用内存地址)。
我们想要a和b都是各自互不影响的,那么只能是完全地新建一个新的对象,并且把现有对象的每个属性的值赋给新的对象的属性。也就是值类型的复制,这个操作就叫深度克隆。
这里我们写两个泛型方法分别对对象T和集合List<T>进行深度克隆的实现,我们的方法实现方式是“扩展方法”,就是能在原有的对象后面直接“点”操作。
下面我们来看一下深度克隆的算法实现:
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; } } }
上述代码已经实现了深度克隆的操作,在使用上我们如下:
例如有User类,我们可以这样操作
User user1=new User();
User user2=user1.DeepCloneObject();
这样就完成了对user1的深度克隆!
【博主声明】
本文为站主原创作品,转载请注明出处:http://www.cnblogs.com/7tiny 且在文章页面明显位置给出原文链接。
作者:
作者:
7tiny
Software Development
北京市海淀区 Haidian Area Beijing 100089,P.R.China
郵箱Email : seventiny@foxmail.com
網址Http: http://www.7tiny.com
WeChat: seven-tiny
更多联系方式点我哦~
Best Regard ~
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?