当前位置: 首页 > news >正文

机械网站怎么做wordpress 博客 视频

机械网站怎么做,wordpress 博客 视频,网站建设的一些销售技巧,互联网保险图片概述#xff1a;以上内容详细介绍了在 C# 中实现不改变原 List 值的多层嵌套复制方法#xff0c;包括使用 AutoMapper、Json.NET、以及对象序列化的步骤和示例。这些方法提供了灵活而高效的方式#xff0c;可以根据项目需求选择最适合的深度复制方式。 1. 使用 AutoMapper …概述以上内容详细介绍了在 C# 中实现不改变原 List 值的多层嵌套复制方法包括使用 AutoMapper、Json.NET、以及对象序列化的步骤和示例。这些方法提供了灵活而高效的方式可以根据项目需求选择最适合的深度复制方式。 1. 使用 AutoMapper 进行多层嵌套复制 AutoMapper 是一个对象映射工具可以方便地进行对象之间的映射。以下是使用 AutoMapper 实现多层嵌套复制的步骤和示例 首先你需要在项目中安装 AutoMapper 包。你可以通过 NuGet 包管理器控制台运行以下命令来安装 Install-Package AutoMapper 然后你可以使用以下代码进行深度复制 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多层嵌套ListStudent originalList new ListStudent{new Student { StudentId 001, Info new Person { Name Alice, Age 25 } },new Student { StudentId 002, Info new Person { Name Bob, Age 30 } }};// 使用 AutoMapper 实现深度复制ListStudent copiedList DeepCopyWithAutoMapper(originalList);// 修改复制后的值copiedList[0].Info.Name Charlie;// 打印原始值验证原始 List 的值是否改变Console.WriteLine(原始 List 的值);PrintList(originalList);// 打印复制后的值Console.WriteLine(\n复制后 List 的值);PrintList(copiedList);}static ListStudent DeepCopyWithAutoMapper(ListStudent originalList){// 初始化 AutoMapper 配置var config new MapperConfiguration(cfg {// 针对每一层嵌套的类型进行映射配置cfg.CreateMapStudent, Student();cfg.CreateMapPerson, Person();});// 创建映射器IMapper mapper config.CreateMapper();// 使用映射器进行深度复制ListStudent newList mapper.MapListStudent(originalList);return newList;}// 打印 List 的方法static void PrintList(ListStudent list){foreach (var student in list){Console.WriteLine($StudentId: {student.StudentId}, Name: {student.Info.Name}, Age: {student.Info.Age});}} } 在这个示例中首先初始化 AutoMapper 配置然后创建映射器并使用映射器进行深度复制。 2. 使用 Json.NET 进行多层嵌套复制 Json.NETNewtonsoft.Json是一个用于处理 JSON 数据的强大库也可以用于实现深度复制。以下是使用 Json.NET 实现多层嵌套复制的步骤和示例 首先你需要在项目中安装 Json.NET 包。你可以通过 NuGet 包管理器控制台运行以下命令来安装 Install-Package Newtonsoft.Json 然后你可以使用以下代码进行深度复制 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多层嵌套ListStudent originalList new ListStudent{new Student { StudentId 001, Info new Person { Name Alice, Age 25 } },new Student { StudentId 002, Info new Person { Name Bob, Age 30 } }};// 使用 Json.NET 实现深度复制ListStudent copiedList DeepCopyWithJson(originalList);// 修改复制后的值copiedList[0].Info.Name Charlie;// 打印原始值验证原始 List 的值是否改变Console.WriteLine(原始 List 的值);PrintList(originalList);// 打印复制后的值Console.WriteLine(\n复制后 List 的值);PrintList(copiedList);}static ListStudent DeepCopyWithJson(ListStudent originalList){// 使用 JsonConvert 进行深度复制string json JsonConvert.SerializeObject(originalList);ListStudent newList JsonConvert.DeserializeObjectListStudent(json);return newList;}// 打印 List 的方法static void PrintList(ListStudent list){foreach(var student in list){Console.WriteLine($StudentId: {student.StudentId}, Name: {student.Info.Name}, Age: {student.Info.Age});}} } 在这个示例中使用 JsonConvert 将原始 List 转换为 JSON 字符串然后再从 JSON 字符串中反序列化得到新的 List实现了深度复制。 3. 使用对象序列化和反序列化进行深度复制 另一种常见的方法是使用 C# 的对象序列化和反序列化功能将对象序列化为字节流然后再反序列化为新的对象。以下是使用序列化和反序列化实现多层嵌套复制的步骤和示例 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多层嵌套ListStudent originalList new ListStudent{new Student { StudentId 001, Info new Person { Name Alice, Age 25 } },new Student { StudentId 002, Info new Person { Name Bob, Age 30 } }};// 使用序列化和反序列化实现深度复制ListStudent copiedList DeepCopyWithSerialization(originalList);// 修改复制后的值copiedList[0].Info.Name Charlie;// 打印原始值验证原始 List 的值是否改变Console.WriteLine(原始 List 的值);PrintList(originalList);// 打印复制后的值Console.WriteLine(\n复制后 List 的值);PrintList(copiedList);}static ListStudent DeepCopyWithSerialization(ListStudent originalList){IFormatter formatter new BinaryFormatter();using (MemoryStream stream new MemoryStream()){formatter.Serialize(stream, originalList);stream.Seek(0, SeekOrigin.Begin);return (ListStudent)formatter.Deserialize(stream);}}// 打印 List 的方法static void PrintList(ListStudent list){foreach (var student in list){Console.WriteLine($StudentId: {student.StudentId}, Name: {student.Info.Name}, Age: {student.Info.Age});}} } 在这个示例中使用 BinaryFormatter 将原始 List 序列化为字节流然后再反序列化得到新的 List实现了深度复制。
http://www.zqtcl.cn/news/136324/

相关文章:

  • 国外经典平面设计网站60平米一居室装修价格
  • 网站建设选择题个人游戏网站备案
  • 深圳企业网站制作公司wordpress 自定义插件开发
  • 网站代付系统怎么做iis不能新建网站
  • 廉政网站建设做环保的网站有哪些
  • 做彩票网站违法网站邮箱后台子域名
  • 响应式中文网站模板wordpress 模特模板
  • 专业做影楼招聘网站有哪些中铁建设集团登陆
  • 室内设计工作室网站怎么做前端开发面试会被问到的一些问题
  • 六安网站建设网络服务30分钟seo网站
  • 网站开发难点谁会制作网站
  • 北京通州网站制作公司设计网站中企动力优
  • 网站地图生成器横琴人寿保险公司官网
  • 免费建站网站一级大录像不卡专业团队建设方案
  • 创建网站的目的是什么想自己建个网站
  • 网站开发公司有什么福利龙岩几个县
  • 网站镜像做排名成都网站开发
  • 江西建设推广网站苏州 网站的公司
  • 中山民众网站建设有一个网站专门做民宿
  • 快速建站完整版兰州兼职做网站
  • 西安网站群搭建php网站开发设计
  • 网站首页没收录php做的网站源代码
  • 网站搭建技术要求企业网站推广的一般策略
  • 网站建设流程行业现状安阳历史
  • 制作软件的网站装饰工程设计东莞网站建设
  • 如何不花钱开发网站搜索引擎营销原理是什么
  • 网站不能访问如何做冗余Wordpress手机短信
  • 深圳的设计网站公司新媒体网站建设
  • 网站title优化实搜网站建设
  • 淘宝网网页版官网优化系统软件