.NET 中的深拷贝实现方法

在 .NET 中实现深拷贝(Deep Copy)有几种常用方法,深拷贝是指创建一个新对象,并递归地复制原对象及其所有引用对象,而不仅仅是复制引用。

目录

  • [1. 使用序列化/反序列化](#1. 使用序列化/反序列化)
  • [2. 使用 JSON 序列化(Newtonsoft.Json 或 System.Text.Json)](#2. 使用 JSON 序列化(Newtonsoft.Json 或 System.Text.Json))
  • [3. 实现 ICloneable 接口(手动实现)](#3. 实现 ICloneable 接口(手动实现))
  • [4. 使用 AutoMapper(适用于复杂对象)](#4. 使用 AutoMapper(适用于复杂对象))
  • [5. 注意事项](#5. 注意事项)
  • [6. 推荐方法](#6. 推荐方法)

1. 使用序列化/反序列化

csharp 复制代码
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public static class ObjectCopier
{
    public static T DeepCopy<T>(T obj)
    {
        if (!typeof(T).IsSerializable)
        {
            throw new ArgumentException("The type must be serializable.", nameof(obj));
        }

        if (ReferenceEquals(obj, null))
        {
            return default;
        }

        IFormatter formatter = new BinaryFormatter();
        using (var stream = new MemoryStream())
        {
            formatter.Serialize(stream, obj);
            stream.Seek(0, SeekOrigin.Begin);
            return (T)formatter.Deserialize(stream);
        }
    }
}

2. 使用 JSON 序列化(Newtonsoft.Json 或 System.Text.Json)

csharp 复制代码
// 使用 Newtonsoft.Json
using Newtonsoft.Json;

public static T DeepCopy<T>(T obj)
{
    var json = JsonConvert.SerializeObject(obj);
    return JsonConvert.DeserializeObject<T>(json);
}

// 使用 System.Text.Json (推荐.NET Core 3.0+)
using System.Text.Json;

public static T DeepCopy<T>(T obj)
{
    var json = JsonSerializer.Serialize(obj);
    return JsonSerializer.Deserialize<T>(json);
}

3. 实现 ICloneable 接口(手动实现)

csharp 复制代码
public class MyClass : ICloneable
{
    public int Value { get; set; }
    public MyOtherClass Other { get; set; }
    
    public object Clone()
    {
        var copy = (MyClass)MemberwiseClone(); // 浅拷贝
        copy.Other = (MyOtherClass)Other.Clone(); // 深拷贝引用类型
        return copy;
    }
}

public class MyOtherClass : ICloneable
{
    public string Name { get; set; }
    
    public object Clone()
    {
        return MemberwiseClone(); // 浅拷贝(因为只有值类型)
    }
}

4. 使用 AutoMapper(适用于复杂对象)

csharp 复制代码
using AutoMapper;

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<MyClass, MyClass>();
    cfg.CreateMap<MyOtherClass, MyOtherClass>();
});

var mapper = config.CreateMapper();
var copy = mapper.Map<MyClass>(original);

5. 注意事项

  1. 序列化方法要求所有相关类都是可序列化的(有 [Serializable] 特性或可以被 JSON 序列化)
  2. 循环引用可能导致堆栈溢出或序列化异常
  3. 性能考虑:对于大型对象图,序列化方法可能较慢
  4. 某些特殊类型(如委托、COM 对象)可能无法正确拷贝

6. 推荐方法

  • 对于简单对象:使用 JSON 序列化(System.Text.Json 性能较好)
  • 对于复杂对象图:考虑实现 ICloneable 或使用 AutoMapper
  • 对于性能敏感场景:考虑手动实现深拷贝逻辑

选择哪种方法取决于具体需求、对象复杂度和性能要求。

相关推荐
huangdong_2 小时前
1688商品图片采集技术解析:登录态处理与SKU图自动分类
开发语言
马士兵教育2 小时前
Java还有前景吗?Java+AI大模型学习路线及项目?
java·人工智能·python·学习·机器学习
chase_my_dream2 小时前
C++ + SLAM 高频面试问题整理
开发语言·c++·面试
snow@li3 小时前
Java:理解 Gradle / 后端项目的管家 / 打包SpringBoot 应用 / 完成编译、下载依赖、运行测试、打包 JAR/WAR / 速查表
java
Cloud_Shy6183 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第五章 Item 30 - 32)
开发语言·人工智能·笔记·python·学习方法
云烟成雨TD3 小时前
Spring AI 1.x 系列【57】动态工具发现:Tool Search Tool
java·人工智能·spring
zfoo-framework3 小时前
[修改代码使用]codex官方app中使用中转(不需要cc-switch) 1.config.toml 2.sk方式登录
java
天佑木枫3 小时前
15天Python入门系列 · 序
开发语言·python
逍遥德3 小时前
MQTT教程详解-05.SpringBoot集成mqtt client 性能分析
java·spring boot·spring·mt
云烟成雨TD3 小时前
Spring AI 1.x 系列【54】Retry 机制分析
java·人工智能·spring