.NET 中的深拷贝实现方法详解
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">1. 使用序列化/反序列化</a></li><li><a href="#_label1">2. 使用 JSON 序列化(Newtonsoft.Json 或 System.Text.Json)</a></li><li><a href="#_label2">3. 实现 ICloneable 接口(手动实现)</a></li><li><a href="#_label3">4. 使用 AutoMapper(适用于复杂对象)</a></li><li><a href="#_label4">5. 注意事项</a></li><li><a href="#_label5">6. 推荐方法</a></li></ul></div><p>在 .NET 中实现深拷贝(Deep Copy)有几种常用方法,深拷贝是指创建一个新对象,并递归地复制原对象及其所有引用对象,而不仅仅是复制引用。</p><p class="maodian"><a name="_label0"></a></p><h2>1. 使用序列化/反序列化</h2>
<div class="jb51code"><pre class="brush: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);
}
}
}</pre></div>
<p class="maodian"><a name="_label1"></a></p><h2>2. 使用 JSON 序列化(Newtonsoft.Json 或 System.Text.Json)</h2>
<div class="jb51code"><pre class="brush: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);
}</pre></div>
<p class="maodian"><a name="_label2"></a></p><h2>3. 实现 ICloneable 接口(手动实现)</h2>
<div class="jb51code"><pre class="brush: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(); // 浅拷贝(因为只有值类型)
}
}</pre></div>
<p class="maodian"><a name="_label3"></a></p><h2>4. 使用 AutoMapper(适用于复杂对象)</h2>
<div class="jb51code"><pre class="brush: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);</pre></div>
<p class="maodian"><a name="_label4"></a></p><h2>5. 注意事项</h2>
<ul><li>序列化方法要求所有相关类都是可序列化的(有 <code></code> 特性或可以被 JSON 序列化)</li><li>循环引用可能导致堆栈溢出或序列化异常</li><li>性能考虑:对于大型对象图,序列化方法可能较慢</li><li>某些特殊类型(如委托、COM 对象)可能无法正确拷贝</li></ul>
<p class="maodian"><a name="_label5"></a></p><h2>6. 推荐方法</h2>
<ul><li>对于简单对象:使用 JSON 序列化(System.Text.Json 性能较好)</li><li>对于复杂对象图:考虑实现 ICloneable 或使用 AutoMapper</li><li>对于性能敏感场景:考虑手动实现深拷贝逻辑</li></ul>
<p>选择哪种方法取决于具体需求、对象复杂度和性能要求。</p>
頁:
[1]