C#?中?List?與?List?多層嵌套不改變原值的實(shí)現(xiàn)方法(深度復(fù)制)
概述:以上內(nèi)容詳細(xì)介紹了在 C# 中實(shí)現(xiàn)不改變原 List 值的多層嵌套復(fù)制方法,包括使用 AutoMapper、Json.NET、以及對象序列化的步驟和示例。這些方法提供了靈活而高效的方式,可以根據(jù)項(xiàng)目需求選擇最適合的深度復(fù)制方式。
1. 使用 AutoMapper 進(jìn)行多層嵌套復(fù)制
AutoMapper 是一個對象映射工具,可以方便地進(jìn)行對象之間的映射。以下是使用 AutoMapper 實(shí)現(xiàn)多層嵌套復(fù)制的步驟和示例:
首先,你需要在項(xiàng)目中安裝 AutoMapper 包。你可以通過 NuGet 包管理器控制臺運(yùn)行以下命令來安裝:
Install-Package AutoMapper
然后,你可以使用以下代碼進(jìn)行深度復(fù)制:
using AutoMapper; using System; using System.Collections.Generic; class Person { public string Name { get; set; } public int Age { get; set; } } class Student { public string StudentId { get; set; } public Person Info { get; set; } } class Program { static void Main() { // 創(chuàng)建原始 List,多層嵌套 List<Student> originalList = new List<Student> { new Student { StudentId = "001", Info = new Person { Name = "Alice", Age = 25 } }, new Student { StudentId = "002", Info = new Person { Name = "Bob", Age = 30 } } }; // 使用 AutoMapper 實(shí)現(xiàn)深度復(fù)制 List<Student> copiedList = DeepCopyWithAutoMapper(originalList); // 修改復(fù)制后的值 copiedList[0].Info.Name = "Charlie"; // 打印原始值,驗(yàn)證原始 List 的值是否改變 Console.WriteLine("原始 List 的值:"); PrintList(originalList); // 打印復(fù)制后的值 Console.WriteLine("\n復(fù)制后 List 的值:"); PrintList(copiedList); } static List<Student> DeepCopyWithAutoMapper(List<Student> originalList) { // 初始化 AutoMapper 配置 var config = new MapperConfiguration(cfg => { // 針對每一層嵌套的類型進(jìn)行映射配置 cfg.CreateMap<Student, Student>(); cfg.CreateMap<Person, Person>(); }); // 創(chuàng)建映射器 IMapper mapper = config.CreateMapper(); // 使用映射器進(jìn)行深度復(fù)制 List<Student> newList = mapper.Map<List<Student>>(originalList); return newList; } // 打印 List 的方法 static void PrintList(List<Student> list) { foreach (var student in list) { Console.WriteLine($"StudentId: {student.StudentId}, Name: {student.Info.Name}, Age: {student.Info.Age}"); } } }
在這個示例中,首先初始化 AutoMapper 配置,然后創(chuàng)建映射器,并使用映射器進(jìn)行深度復(fù)制。
2. 使用 Json.NET 進(jìn)行多層嵌套復(fù)制
Json.NET(Newtonsoft.Json)是一個用于處理 JSON 數(shù)據(jù)的強(qiáng)大庫,也可以用于實(shí)現(xiàn)深度復(fù)制。以下是使用 Json.NET 實(shí)現(xiàn)多層嵌套復(fù)制的步驟和示例:
首先,你需要在項(xiàng)目中安裝 Json.NET 包。你可以通過 NuGet 包管理器控制臺運(yùn)行以下命令來安裝:
Install-Package Newtonsoft.Json
然后,你可以使用以下代碼進(jìn)行深度復(fù)制:
using Newtonsoft.Json; using System; using System.Collections.Generic; class Person { public string Name { get; set; } public int Age { get; set; } } class Student { public string StudentId { get; set; } public Person Info { get; set; } } class Program { static void Main() { // 創(chuàng)建原始 List,多層嵌套 List<Student> originalList = new List<Student> { new Student { StudentId = "001", Info = new Person { Name = "Alice", Age = 25 } }, new Student { StudentId = "002", Info = new Person { Name = "Bob", Age = 30 } } }; // 使用 Json.NET 實(shí)現(xiàn)深度復(fù)制 List<Student> copiedList = DeepCopyWithJson(originalList); // 修改復(fù)制后的值 copiedList[0].Info.Name = "Charlie"; // 打印原始值,驗(yàn)證原始 List 的值是否改變 Console.WriteLine("原始 List 的值:"); PrintList(originalList); // 打印復(fù)制后的值 Console.WriteLine("\n復(fù)制后 List 的值:"); PrintList(copiedList); } static List<Student> DeepCopyWithJson(List<Student> originalList) { // 使用 JsonConvert 進(jìn)行深度復(fù)制 string json = JsonConvert.SerializeObject(originalList); List<Student> newList = JsonConvert.DeserializeObject<List<Student>>(json); return newList; } // 打印 List 的方法 static void PrintList(List<Student> list) { foreach (var student in list) { Console.WriteLine($"StudentId: {student.StudentId}, Name: {student.Info.Name}, Age: {student.Info.Age}"); } } }
在這個示例中,使用 JsonConvert 將原始 List 轉(zhuǎn)換為 JSON 字符串,然后再從 JSON 字符串中反序列化得到新的 List,實(shí)現(xiàn)了深度復(fù)制。
3. 使用對象序列化和反序列化進(jìn)行深度復(fù)制
另一種常見的方法是使用 C# 的對象序列化和反序列化功能,將對象序列化為字節(jié)流,然后再反序列化為新的對象。以下是使用序列化和反序列化實(shí)現(xiàn)多層嵌套復(fù)制的步驟和示例:
using System; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; class Person { public string Name { get; set; } public int Age { get; set; } } class Student { public string StudentId { get; set; } public Person Info { get; set; } } class Program { static void Main() { // 創(chuàng)建原始 List,多層嵌套 List<Student> originalList = new List<Student> { new Student { StudentId = "001", Info = new Person { Name = "Alice", Age = 25 } }, new Student { StudentId = "002", Info = new Person { Name = "Bob", Age = 30 } } }; // 使用序列化和反序列化實(shí)現(xiàn)深度復(fù)制 List<Student> copiedList = DeepCopyWithSerialization(originalList); // 修改復(fù)制后的值 copiedList[0].Info.Name = "Charlie"; // 打印原始值,驗(yàn)證原始 List 的值是否改變 Console.WriteLine("原始 List 的值:"); PrintList(originalList); // 打印復(fù)制后的值 Console.WriteLine("\n復(fù)制后 List 的值:"); PrintList(copiedList); } static List<Student> DeepCopyWithSerialization(List<Student> originalList) { IFormatter formatter = new BinaryFormatter(); using (MemoryStream stream = new MemoryStream()) { formatter.Serialize(stream, originalList); stream.Seek(0, SeekOrigin.Begin); return (List<Student>)formatter.Deserialize(stream); } } // 打印 List 的方法 static void PrintList(List<Student> list) { foreach (var student in list) { Console.WriteLine($"StudentId: {student.StudentId}, Name: {student.Info.Name}, Age: {student.Info.Age}"); } } }
在這個示例中,使用 BinaryFormatter 將原始 List 序列化為字節(jié)流,然后再反序列化得到新的 List,實(shí)現(xiàn)了深度復(fù)制。
到此這篇關(guān)于C# 中 List 與 List 多層嵌套不改變原值的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)C# List 多層嵌套內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
winform中的ListBox和ComboBox綁定數(shù)據(jù)用法實(shí)例
這篇文章主要介紹了winform中的ListBox和ComboBox綁定數(shù)據(jù)用法,實(shí)例分析了將集合數(shù)據(jù)綁定到ListBox和ComboBox控件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2014-12-12C#向PPT文檔插入圖片以及導(dǎo)出圖片的實(shí)例
PowerPoint演示文稿是我們?nèi)粘9ぷ髦谐S玫霓k公軟件之一,本篇文章介紹了C#向PPT文檔插入圖片以及導(dǎo)出圖片的實(shí)例,非常具有實(shí)用價值,需要的朋友可以參考下。2016-12-12C#基于Socket的TCP通信實(shí)現(xiàn)聊天室案例
這篇文章主要為大家詳細(xì)介紹了C#基于Socket的TCP通信實(shí)現(xiàn)聊天室案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02C#中const,readonly和static關(guān)鍵字的用法介紹
這篇文章介紹了C#中const,readonly和static關(guān)鍵字的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-0810分鐘學(xué)會Visual Studio將自己創(chuàng)建的類庫打包到NuGet進(jìn)行引用(net,net core,C#)
這篇文章主要介紹了10分鐘學(xué)會Visual Studio將自己創(chuàng)建的類庫打包到NuGet進(jìn)行引用(net,net core,C#),本文給大家介紹的非常詳細(xì)對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09WinForm項(xiàng)目開發(fā)中Excel用法實(shí)例解析
這篇文章主要介紹了WinForm項(xiàng)目開發(fā)中Excel用法,非常實(shí)用,需要的朋友可以參考下2014-08-08c#之圓形無標(biāo)題欄橢圓窗體的實(shí)現(xiàn)詳解
本篇文章是對c#中圓形無標(biāo)題欄橢圓窗體的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06