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

自己做卖东西网站网站建设的项目计划

自己做卖东西网站,网站建设的项目计划,推广方案格式模板范文,手机网站开发怎么收费概述#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/557316/

相关文章:

  • html5 js全屏滑动网站源码wordpress 插件 破解
  • 做电影网站怎么批量去水印微信用什么小程序可以提取文字
  • 网站开发费用周期域名网站建设方案书模板
  • 织梦网站问题关于政务网站建设工作情况的总结
  • wordpress 拿站网站搭建后如何使用
  • 网站设计应遵循的原则wordpress免费空间
  • 建设网站的特色企业内部培训app软件
  • jsp网站缓存在哪wordpress设置静态页面
  • 百度做网站电话多少东台网页定制
  • 暖通设计网站推荐百度在西安的公司叫什么
  • 天津响应式网站设计网站建设的任务
  • 多语言网站 自助江门建设局网站
  • 宜兴做阿里巴巴网站常州外贸网站设计
  • 长沙米拓建站wordpress最底部版权
  • 小企业网站建设费用一年wordpress 怎么登陆
  • 阿里云建站可不可以备案wordpress评论滑动插件
  • 网站建设教程出售用苏州久远网络现在的网络营销方式
  • 浮动播放器wordpress刷神马seo排名首页排名
  • 建设网站呼叫中心有什么好处站长统计免费下载
  • 做网站电脑配置要求个高吗电子商务网站的建设包含哪些流程图
  • 青岛做网站大公司wordpress文章页加一言
  • 用哪个网站做相册视频文件夹谷歌ads
  • 注册top域名做公司网站男女做暖网站是什么
  • 郴州本地网站建设高端网站设计多少钱
  • 此网站域名即将过期广告制作属于什么行业
  • 牛牛襄阳网站建设wap网站asp源码
  • 信用网站建设招标书建网站需要什么手续
  • 重庆建网站方法网站开发和维护
  • 做网站需要什么人活动策划流程及细节
  • wordpress企业网站seo上海市