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

seo网站文章编辑软件营销网站开发公司

seo网站文章编辑软件,营销网站开发公司,flash 网站开发教程,网站制作公司在哪里找前言在开发中#xff0c;我们经常需要创建某个类型实例的副本。常用的方式#xff0c;是继承ICloneable接口#xff0c;然后自行实现Clone()#xff0c;这会耗费一定的开发时间#xff1b;或者使用序列化/反序列化方式变相实现#xff0c;但是性能不高。现在#xff0c;… 前言在开发中我们经常需要创建某个类型实例的副本。常用的方式是继承ICloneable接口然后自行实现Clone()这会耗费一定的开发时间或者使用序列化/反序列化方式变相实现但是性能不高。现在可以尝试用Source Generators实现。实现思路首先需要Clone的类必须声明一个特定的CloneableAttribute这样Source Generators才知道为谁实现Clone方法。然后Source Generators遍历该类型的所有属性为其编写属性赋值代码。如果属性本身也是Cloneable类型那就调用属性对应类型的Clone方法实现深度克隆。具体代码1.添加CloneableAttribute向待编译项目加入CloneableAttribute代码const string cloneableAttributeText  using System;namespace CloneableDemo {public sealed class CloneableAttribute : Attribute{public CloneableAttribute(){}} } ;context.AddSource(CloneableAttribute, SourceText.From(cloneableAttributeText, Encoding.UTF8));2.遍历CloneableAttribute声明类找到声明了CloneableAttribute的所有类型var cloneableAttribute  compilation.GetTypeByMetadataName(CloneableDemo.CloneableAttribute); foreach (var classSymbol in classSymbols) {if (!classSymbol.TryGetAttribute(cloneableAttribute, out var attributes))continue;context.AddSource(${classSymbol.Name}_clone.cs, SourceText.From(CreateCloneCode(classSymbol), Encoding.UTF8)); }3.生成Clone代码遍历属性生成Clone方法private string CreateCloneableCode(INamedTypeSymbol classSymbol){string namespaceName  classSymbol.ContainingNamespace.ToDisplayString();var propertyNames  classSymbol.GetMembers().OfTypeIPropertySymbol();var codes  new StringBuilder();foreach (var propertyName in propertyNames){if (isCloneable(propertyName)){codes.AppendLine($                {propertyName}  obj.{propertyName}?.Clone(),);}else{codes.AppendLine($                {propertyName}  obj.{propertyName},);}}return $using System.Collections.Generic;namespace {namespaceName} {{public static class {classSymbol.Name}Extentions{{public static {classSymbol.Name} Clone(this {classSymbol.Name} obj){{return new {classSymbol.Name}{{{codes.ToString()}}};}}}} }};}4.使用现在就可以在目标项目中使用Clone方法了:[Cloneable] public class Class1 {public string A { get; set; }public Class2 B { get; set; } }[Cloneable] public class Class2 {public string A { get; set; } }var obj  new Class2() {A  My IO, }; var deep  new Class1() {A  My IO,B  obj }; var clone  deep.Clone();结论有了Source Generators可以让编译器帮我们自动实现Clone方法既节约了开发时间又保证了性能如果你觉得这篇文章对你有所启发请关注我的个人公众号”My IO“
http://www.yutouwan.com/news/181293/

相关文章:

  • 商丘网站制作公司一二三网络推广土石方工程网站
  • wordpress中文企业网站中山网络公关
  • 网站如何做留言板推广公司如何赚钱
  • 海口装饰设计网站建设做门票的网站
  • 彩票站自己做网站永康网站推广
  • 珠海企业集团网站建设力洋深圳做网站公司
  • 广州个人网站备案要多久游戏代理是做什么的
  • 婚纱摄影网站建设大概多少钱做电商一件代发的网站
  • 重庆网站建设哪家公司好官网静态html模板
  • 福建网站设计制作直播网站
  • 在北京建网站在网站上上传文件需要怎么做
  • 项目外包合同短视频seo公司
  • 做彩票网站需要什么技术网站开发四点注意事项
  • 建设一个购物网站多少钱专门做外卖的网站
  • 南翔企业网站开发建设wordpress自动创建子站
  • 定制网站建设多少钱小白如何免费做网站
  • 免费网站建站手机seo营销外包
  • 哪种企业适合做网站动漫设计专升本考什么
  • dedecms的网站放在哪个文件夹里大连模板网站制作报价
  • wordpress 网站主题苏州企业网站公司都有哪些
  • 网站上怎么做微信支付接口网站标题改动
  • 南宁南宁做网站郑州企业网站排名优化哪家好
  • 个人网站鉴赏营销型网站建设的选择方式
  • 天津网站专业制作制作流程图软件
  • 3d云设计网站开发西安有什么好玩的地方景点推荐
  • 门户网站ui设计课程资源网站的建设
  • 公司网站开发策划北京建设信源资讯有限公司
  • 个人网站建设代码揭阳市php网站开发找工作
  • 中企动力网站方案什么是口碑营销
  • 建设彩票网站制作上海建设工程安全质量监督总站网站