深度復(fù)制:C# 中 List 與 List 多層嵌套不改變?cè)档膶?shí)現(xiàn)方法

概述:以下內(nèi)容詳細(xì)介紹了在 C# 中實(shí)現(xiàn)不改變?cè)?List 值的多層嵌套復(fù)制方法,包括使用 AutoMapper、Json.NET、以及對(duì)象序列化的步驟和示例。這些方法提供了靈活而高效的方式,可以根據(jù)項(xiàng)目需求選擇最適合的深度復(fù)制方式。
1. 使用 AutoMapper 進(jìn)行多層嵌套復(fù)制
AutoMapper 是一個(gè)對(duì)象映射工具,可以方便地進(jìn)行對(duì)象之間的映射。以下是使用 AutoMapper 實(shí)現(xiàn)多層嵌套復(fù)制的步驟和示例:
首先,你需要在項(xiàng)目中安裝 AutoMapper 包。你可以通過(guò) NuGet 包管理器控制臺(tái)運(yùn)行以下命令來(lái)安裝:
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 =>
{
// 針對(duì)每一層嵌套的類型進(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}");
}
}
}在這個(gè)示例中,首先初始化 AutoMapper 配置,然后創(chuàng)建映射器,并使用映射器進(jìn)行深度復(fù)制。
2. 使用 Json.NET 進(jìn)行多層嵌套復(fù)制
Json.NET(Newtonsoft.Json)是一個(gè)用于處理 JSON 數(shù)據(jù)的強(qiáng)大庫(kù),也可以用于實(shí)現(xiàn)深度復(fù)制。以下是使用 Json.NET 實(shí)現(xiàn)多層嵌套復(fù)制的步驟和示例:
首先,你需要在項(xiàng)目中安裝 Json.NET 包。你可以通過(guò) NuGet 包管理器控制臺(tái)運(yùn)行以下命令來(lái)安裝:
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}");
}
}
}在這個(gè)示例中,使用 JsonConvert 將原始 List 轉(zhuǎn)換為 JSON 字符串,然后再?gòu)?JSON 字符串中反序列化得到新的 List,實(shí)現(xiàn)了深度復(fù)制。
3. 使用對(duì)象序列化和反序列化進(jìn)行深度復(fù)制
另一種常見(jiàn)的方法是使用 C# 的對(duì)象序列化和反序列化功能,將對(duì)象序列化為字節(jié)流,然后再反序列化為新的對(duì)象。以下是使用序列化和反序列化實(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}");
}
}
}在這個(gè)示例中,使用 BinaryFormatter 將原始 List 序列化為字節(jié)流,然后再反序列化得到新的 List,實(shí)現(xiàn)了深度復(fù)制。























