AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

深度復制:C# 中 List 與 List 多層嵌套不改變原值的實現方法

來源: 責編: 時間:2024-05-23 17:13:38 147觀看
導讀概述:以下內容詳細介紹了在 C# 中實現不改變原 List 值的多層嵌套復制方法,包括使用 AutoMapper、Json.NET、以及對象序列化的步驟和示例。這些方法提供了靈活而高效的方式,可以根據項目需求選擇最適合的深度復制方式。1

Nvi28資訊網——每日最新資訊28at.com

概述:以下內容詳細介紹了在 C# 中實現不改變原 List 值的多層嵌套復制方法,包括使用 AutoMapper、Json.NET、以及對象序列化的步驟和示例。這些方法提供了靈活而高效的方式,可以根據項目需求選擇最適合的深度復制方式。Nvi28資訊網——每日最新資訊28at.com

1. 使用 AutoMapper 進行多層嵌套復制

AutoMapper 是一個對象映射工具,可以方便地進行對象之間的映射。以下是使用 AutoMapper 實現多層嵌套復制的步驟和示例:Nvi28資訊網——每日最新資訊28at.com

首先,你需要在項目中安裝 AutoMapper 包。你可以通過 NuGet 包管理器控制臺運行以下命令來安裝:Nvi28資訊網——每日最新資訊28at.com

Install-Package AutoMapper

然后,你可以使用以下代碼進行深度復制:Nvi28資訊網——每日最新資訊28at.com

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()    {        // 創建原始 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 實現深度復制        List<Student> copiedList = DeepCopyWithAutoMapper(originalList);        // 修改復制后的值        copiedList[0].Info.Name = "Charlie";        // 打印原始值,驗證原始 List 的值是否改變        Console.WriteLine("原始 List 的值:");        PrintList(originalList);        // 打印復制后的值        Console.WriteLine("/n復制后 List 的值:");        PrintList(copiedList);    }    static List<Student> DeepCopyWithAutoMapper(List<Student> originalList)    {        // 初始化 AutoMapper 配置        var config = new MapperConfiguration(cfg =>        {            // 針對每一層嵌套的類型進行映射配置            cfg.CreateMap<Student, Student>();            cfg.CreateMap<Person, Person>();        });        // 創建映射器        IMapper mapper = config.CreateMapper();        // 使用映射器進行深度復制        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 配置,然后創建映射器,并使用映射器進行深度復制。Nvi28資訊網——每日最新資訊28at.com

2. 使用 Json.NET 進行多層嵌套復制

Json.NET(Newtonsoft.Json)是一個用于處理 JSON 數據的強大庫,也可以用于實現深度復制。以下是使用 Json.NET 實現多層嵌套復制的步驟和示例:Nvi28資訊網——每日最新資訊28at.com

首先,你需要在項目中安裝 Json.NET 包。你可以通過 NuGet 包管理器控制臺運行以下命令來安裝:Nvi28資訊網——每日最新資訊28at.com

Install-Package Newtonsoft.Json

然后,你可以使用以下代碼進行深度復制:Nvi28資訊網——每日最新資訊28at.com

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()    {        // 創建原始 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 實現深度復制        List<Student> copiedList = DeepCopyWithJson(originalList);        // 修改復制后的值        copiedList[0].Info.Name = "Charlie";        // 打印原始值,驗證原始 List 的值是否改變        Console.WriteLine("原始 List 的值:");        PrintList(originalList);        // 打印復制后的值        Console.WriteLine("/n復制后 List 的值:");        PrintList(copiedList);    }    static List<Student> DeepCopyWithJson(List<Student> originalList)    {        // 使用 JsonConvert 進行深度復制        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 轉換為 JSON 字符串,然后再從 JSON 字符串中反序列化得到新的 List,實現了深度復制。Nvi28資訊網——每日最新資訊28at.com

3. 使用對象序列化和反序列化進行深度復制

另一種常見的方法是使用 C# 的對象序列化和反序列化功能,將對象序列化為字節流,然后再反序列化為新的對象。以下是使用序列化和反序列化實現多層嵌套復制的步驟和示例:Nvi28資訊網——每日最新資訊28at.com

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()    {        // 創建原始 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 } }        };        // 使用序列化和反序列化實現深度復制        List<Student> copiedList = DeepCopyWithSerialization(originalList);        // 修改復制后的值        copiedList[0].Info.Name = "Charlie";        // 打印原始值,驗證原始 List 的值是否改變        Console.WriteLine("原始 List 的值:");        PrintList(originalList);        // 打印復制后的值        Console.WriteLine("/n復制后 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 序列化為字節流,然后再反序列化得到新的 List,實現了深度復制。Nvi28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-90351-0.html深度復制:C# 中 List 與 List 多層嵌套不改變原值的實現方法

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 如何用Python輕松檢測文本相似性:原理與方法

下一篇: Tailwind 4.0 即將發布,看起來很不錯!

標簽:
  • 熱門焦點
Top