.NET 中的深拷貝實(shí)現(xiàn)方法詳解
在 .NET 中實(shí)現(xiàn)深拷貝(Deep Copy)有幾種常用方法,深拷貝是指創(chuàng)建一個(gè)新對(duì)象,并遞歸地復(fù)制原對(duì)象及其所有引用對(duì)象,而不僅僅是復(fù)制引用。
1. 使用序列化/反序列化
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)
// 使用 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. 實(shí)現(xiàn) ICloneable 接口(手動(dòng)實(shí)現(xiàn))
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(); // 淺拷貝(因?yàn)橹挥兄殿愋停?
}
}4. 使用 AutoMapper(適用于復(fù)雜對(duì)象)
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. 注意事項(xiàng)
- 序列化方法要求所有相關(guān)類都是可序列化的(有
[Serializable]特性或可以被 JSON 序列化) - 循環(huán)引用可能導(dǎo)致堆棧溢出或序列化異常
- 性能考慮:對(duì)于大型對(duì)象圖,序列化方法可能較慢
- 某些特殊類型(如委托、COM 對(duì)象)可能無法正確拷貝
6. 推薦方法
- 對(duì)于簡單對(duì)象:使用 JSON 序列化(System.Text.Json 性能較好)
- 對(duì)于復(fù)雜對(duì)象圖:考慮實(shí)現(xiàn) ICloneable 或使用 AutoMapper
- 對(duì)于性能敏感場(chǎng)景:考慮手動(dòng)實(shí)現(xiàn)深拷貝邏輯
選擇哪種方法取決于具體需求、對(duì)象復(fù)雜度和性能要求。
到此這篇關(guān)于.NET 中的深拷貝實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān).net深拷貝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
System.Timers.Timer定時(shí)執(zhí)行程序示例代碼
如果是某個(gè)邏輯功能的定時(shí),可以將code放到邏輯功能的類的靜態(tài)構(gòu)造函數(shù)中,在該邏輯類第一次執(zhí)行時(shí),靜態(tài)構(gòu)造函數(shù)會(huì)被調(diào)用,則定時(shí)自然啟動(dòng)2013-06-06
ASP.NET中ListView(列表視圖)的使用前臺(tái)綁定附源碼
ListView(列表視圖)想必大家都知道吧,接下來本文將介紹下ListView的使用前臺(tái)綁定,感興趣的你可不要錯(cuò)過本文了哈2013-03-03
.net core 6.0 通過依賴注入注冊(cè)和使用上下文服務(wù)的教程
在.NET Core 6.0 中,獲取上下文的方式取決于您使用的技術(shù)棧和具體的應(yīng)用程序類型,這篇文章主要介紹了.net core 6.0 通過依賴注入注冊(cè)和使用上下文服務(wù)的教程,需要的朋友可以參考下2023-12-12
Asp.Net、asp實(shí)現(xiàn)的搜索引擎網(wǎng)址收錄檢查程序
這篇文章主要介紹了Asp.Net、asp實(shí)現(xiàn)的搜索引擎網(wǎng)址收錄檢查程序,即實(shí)現(xiàn)檢查一個(gè)網(wǎng)址是否被搜索引擎收錄功能的小程序,需要的朋友可以參考下2014-08-08
.NET?Core中配置Configuration的學(xué)習(xí)指南
.NET中的配置,本質(zhì)上就是key-value鍵值對(duì),并且key和value都是字符串類型,這篇文章主要為大家介紹了.NET?Core配置Configuration的相關(guān)知識(shí),希望對(duì)大家有一定的幫助2025-04-04
MVC默認(rèn)路由實(shí)現(xiàn)分頁(PagerExtend.dll下載)
這篇文章主要介紹了MVC默認(rèn)路由實(shí)現(xiàn)分頁,采用bootstrap的樣式,文末提供了PagerExtend.dll下載地址,感興趣的小伙伴們可以參考一下2016-07-07

