C# 使用反射来实现对象的深度复制方法
作者:jingxian 时间:2021-10-27 19:50:01
实现方式
通过挨个罗列的方式一次复制子对象是非常耗费人力的,如果子对象是引用类型,则还要需要考虑是否对子对象进一步深拷贝。
实际应用中,一个类如果有几十个子对象,挨个复制对于开发人员来说索然无味比较费时费力。
所以使用反射机制来实现。
但是如果是服务端运行的话,还是建议手动的实现。
毕竟反射机制比直接写出来的效率要慢一些。
代码:
public static class DeepCopyHelper
{
public static object Copy(this object obj)
{
Object targetDeepCopyObj;
Type targetType = obj.GetType();
//值类型
if (targetType.IsValueType == true)
{
targetDeepCopyObj = obj;
}
//引用类型
else
{
targetDeepCopyObj = System.Activator.CreateInstance(targetType); //创建引用对象
System.Reflection.MemberInfo[] memberCollection = obj.GetType().GetMembers();
foreach (System.Reflection.MemberInfo member in memberCollection)
{
if (member.MemberType == System.Reflection.MemberTypes.Field)
{
System.Reflection.FieldInfo field = (System.Reflection.FieldInfo)member;
Object fieldValue = field.GetValue(obj);
if (fieldValue is ICloneable)
{
field.SetValue(targetDeepCopyObj, (fieldValue as ICloneable).Clone());
}
else
{
field.SetValue(targetDeepCopyObj, Copy(fieldValue));
}
}
else if (member.MemberType == System.Reflection.MemberTypes.Property)
{
System.Reflection.PropertyInfo myProperty = (System.Reflection.PropertyInfo)member;
MethodInfo info = myProperty.GetSetMethod(false);
if (info != null)
{
object propertyValue = myProperty.GetValue(obj, null);
if (propertyValue is ICloneable)
{
myProperty.SetValue(targetDeepCopyObj, (propertyValue as ICloneable).Clone(), null);
}
else
{
myProperty.SetValue(targetDeepCopyObj, Copy(propertyValue), null);
}
}
}
}
}
return targetDeepCopyObj;
}
}
标签:C#,反射,对象,深度复制
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MyBatis-plus实现逆向生成器
2021-08-26 16:37:14
![](https://img.aspxhome.com/file/2023/0/122300_0s.png)
Unity实现仿3D轮转图效果
2023-11-24 12:26:56
![](https://img.aspxhome.com/file/2023/7/83247_0s.gif)
Android编程动态按钮实现方法
2021-12-31 18:05:17
C#利用VS中插件打包并发布winfrom程序
2022-01-01 21:18:22
![](https://img.aspxhome.com/file/2023/6/91336_0s.jpg)
java 如何判断是否是26个英文字母
2023-11-29 15:51:59
Intellij IDEA中启动多个微服务(开启Run Dashboard管理)
2022-01-11 02:25:00
![](https://img.aspxhome.com/file/2023/8/62708_0s.png)
SSM框架实现分页和搜索分页的示例代码
2022-04-13 04:52:49
Android蓝牙服务查找附近设备分析探索
2023-04-01 19:50:21
C#执行EXE文件与输出消息的提取操作
2023-02-28 00:08:06
![](https://img.aspxhome.com/file/2023/7/85717_0s.jpg)
简单记事本java源码实例
2023-11-26 02:03:17
![](https://img.aspxhome.com/file/2023/4/59744_0s.jpg)
Java流式操作之Collectors工具类操作指南
2022-12-01 01:25:55
![](https://img.aspxhome.com/file/2023/9/72389_0s.png)
JDBC基础教程
2021-10-26 03:47:09
不用IDE写C#的Hello World的方法
2022-01-25 19:24:58
![](https://img.aspxhome.com/file/2023/1/91281_0s.png)
全网最深分析SpringBoot MVC自动配置失效的原因
2021-07-20 03:53:25
![](https://img.aspxhome.com/file/2023/4/65214_0s.jpg)
Spring MVC过滤器-登录过滤的代码实现
2021-06-05 15:01:43
C# 关于爬取网站数据遇到csrf-token的分析与解决
2023-07-25 06:25:39
![](https://img.aspxhome.com/file/2023/2/96352_0s.png)
C#获取指定PDF文件页数的方法
2021-10-10 17:15:43
java web监听器统计在线用户及人数
2023-12-14 14:42:52
![](https://img.aspxhome.com/file/2023/2/62842_0s.jpg)
Java HelloWorld原理分析_动力节点Java学院整理
2023-05-23 21:34:14
Android编程实现自定义系统菜单背景的方法
2023-07-16 22:02:51
![](https://img.aspxhome.com/file/2023/4/111674_0s.jpg)