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

购物网站建设需求模板下载广州seo外包多少钱

购物网站建设需求模板下载,广州seo外包多少钱,wordpress 图片水印插件,高德街景地图全景下载本文章主要介绍C#中对象的拷贝,其中包括浅拷贝和深拷贝,以及浅拷贝和深拷贝的实现方式,不同的实现方式之间的性能对比。 1、浅拷贝和深拷贝 浅拷贝是指将对象中的数值类型的字段拷贝到新的对象中,而对象中的引用型字段则指复制它…

本文章主要介绍C#中对象的拷贝,其中包括浅拷贝和深拷贝,以及浅拷贝和深拷贝的实现方式,不同的实现方式之间的性能对比。

 

1、浅拷贝和深拷贝

浅拷贝是指将对象中的数值类型的字段拷贝到新的对象中,而对象中的引用型字段则指复制它的一个引用到目标对象。如果改变目标对象中引用型字段的值他将反映在原是对象中,也就是说原始对象中对应的字段也会发生变化。深拷贝与浅拷贝不同的是对于引用的处理,深拷贝将会在新对象中创建一个新的和原是对象中对应字段相同(内容相同)的字段,也就是说这个引用和原是对象的引用是不同的,我们在改变新对象中的这个字段的时候是不会影响到原始对象中对应字段的内容。

2、浅拷贝实现方式

1)new对象赋值

[Serializable]
class Employee
{public string ID { get; set; }public int Age { get; set; }public Department DepartmentName { get; set; }
}
[Serializable]
class Department
{public string DepartmentName { get; set; }public Department(string value){DepartmentName = value;}public override string ToString(){return DepartmentName.ToString();}
}
Employee emp1 = new Employee()
{ID = "cjavapy",Age = 20,DepartmentName = new Department("develop")
}
;
Employee emp2=new Employee()
{ID=emp1.ID,Age=emp1.Age,DepartmentName=emp1.DepartmentName
};

 2)实现ICloneable接口

class Employee : ICloneable
{public string ID { get; set; }public int Age { get; set; }public Department DepartmentName { get; set; }//实现ICloneable接口的Clone方法public object Clone(){return this.MemberwiseClone();//浅拷贝}
}
class Department
{public string DepartmentName { get; set; }public Department(string value){DepartmentName = value;}public override string ToString(){return DepartmentName.ToString();}
}

 浅拷贝:

Employee emp1 = new Employee()
{ID = "cjavapy",Age = 20,DepartmentName = new Department("develop")
};
Employee emp2 = emp1.Clone() as Employee;//浅拷贝

 

3、深拷贝实现方式

1)二进制序列化

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using Newtonsoft.Json;namespace ConsoleApplication
{public class Utils{public static T BinaryClone<T>(T source){if (!typeof(T).IsSerializable){throw new ArgumentException("需要添加[Serializable]标签", "source");}if (Object.ReferenceEquals(source, null)){return default(T);}IFormatter formatter = new BinaryFormatter();Stream stream = new MemoryStream();using (stream){formatter.Serialize(stream, source);stream.Seek(0, SeekOrigin.Begin);return (T)formatter.Deserialize(stream);}}}}

 2)JSON序列化

using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using Newtonsoft.Json;namespace ConsoleApplication
{public class Utils{/// /// 序列化反序列化方式/// /// /// public static TOut JsonClone<TIn,TOut>(TIn tIn){return JsonConvert.DeserializeObject<TOut>(JsonConvert.SerializeObject(tIn));}}}

 3)Reflection反射

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApplication
{public class Utils{/// /// 反射实现深拷贝/// /// 传入TIn对象返回TOut对象/// public static TOut ReflectionClone<TIn, TOut>(TIn tIn){TOut tOut = Activator.CreateInstance<TOut>();foreach (var itemOut in tOut.GetType().GetProperties()){var propIn = tIn.GetType().GetProperty(itemOut.Name);itemOut.SetValue(tOut, propIn.GetValue(tIn));}foreach (var itemOut in tOut.GetType().GetFields()){var fieldIn = tIn.GetType().GetField(itemOut.Name);itemOut.SetValue(tOut, fieldIn.GetValue(tIn));}return tOut;}/// <summary>/// 传入List<TIn>,返回List<TOut>/// </summary>/// <typeparam name="TIn"></typeparam>/// <typeparam name="TOut"></typeparam>/// <param name="tInList"></param>/// <returns></returns>public static List<TOut> ReflectionCloneList<TIn, TOut>(List<TIn> tInList){List<TOut> result = new List<TOut>();foreach (var tIn in tInList){TOut tOut = Activator.CreateInstance<TOut>();foreach (var itemOut in tOut.GetType().GetProperties()){var propIn = tIn.GetType().GetProperty(itemOut.Name);itemOut.SetValue(tOut, propIn.GetValue(tIn));}foreach (var itemOut in tOut.GetType().GetFields()){var fieldIn = tIn.GetType().GetField(itemOut.Name);itemOut.SetValue(tOut, fieldIn.GetValue(tIn));}result.Add(tOut);}return result;}}public class ContactPerson{public string Name { get; set; }public string MobileNum { get; set; }}class Program{static void Main(string[] args){var persons = new List<ContactPerson>{new ContactPerson { Name= "C", MobileNum = "13756863001"},new ContactPerson { Name = "C#", MobileNum = "13756863002"},new ContactPerson { Name = "Java", MobileNum = "13756863003"}};var result = Utils.ReflectionCloneList<ContactPerson, ContactPerson>(persons);foreach(var p in result)Console.WriteLine("姓名: {0} 号码为: {1}", p.Name, p.MobileNum);Console.Read();}}
}

 4)XML序列化

using System.IO;
using System.Xml.Serialization;namespace ConsoleApplication
{public class Utils{public static T DeserializeXML<T>(string xmlData) where T : new(){if (string.IsNullOrEmpty(xmlData))return default(T);TextReader tr = new StringReader(xmlData);T DocItms = new T();XmlSerializer xms = new XmlSerializer(DocItms.GetType());DocItms = (T)xms.Deserialize(tr);return DocItms == null ? default(T) : DocItms;}}
}

 5)表达式目录树

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApplication
{/// <summary>/// 生成表达式目录树  泛型缓存/// </summary>/// <typeparam name="TIn"></typeparam>/// <typeparam name="TOut"></typeparam>public class ExpressionGenericMapper<TIn, TOut>//`2{private static Func<TIn, TOut> _FUNC = null;static ExpressionGenericMapper(){ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");List<MemberBinding> memberBindingList = new List<MemberBinding>();foreach (var item in typeof(TOut).GetProperties()){MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));MemberBinding memberBinding = Expression.Bind(item, property);memberBindingList.Add(memberBinding);}foreach (var item in typeof(TOut).GetFields()){MemberExpression property = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));MemberBinding memberBinding = Expression.Bind(item, property);memberBindingList.Add(memberBinding);}MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]{parameterExpression});_FUNC = lambda.Compile();//}public static TOut Trans(TIn t){return _FUNC(t);}}
}

 

4、拷贝方式性能对比

对比代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using Newtonsoft.Json;namespace ConsoleApplication
{class Program{static void Main(string[] args){Employee emp1 = new Employee(){ID = "cjavapy",Age = 20,DepartmentName = new Department("develop")};long common = 0;long expression = 0;long json = 0;long xml = 0;long binary = 0;long reflection = 0;{Stopwatch watch = new Stopwatch();watch.Start();for (int i = 0; i < 1000000; i++){Employee copy = BinaryClone<Employee>(emp1);}watch.Stop();binary = watch.ElapsedMilliseconds;}{Stopwatch watch = new Stopwatch();watch.Start();for (int i = 0; i < 1000000; i++){Employee copy = ReflectionClone<Employee, Employee>(emp1);}watch.Stop();reflection = watch.ElapsedMilliseconds;}{Stopwatch watch = new Stopwatch();watch.Start();for (int i = 0; i < 1000000; i++){Employee copy = DeserializeXML<Employee>(SerializeXML<Employee>(emp1));}watch.Stop();xml = watch.ElapsedMilliseconds;}{Stopwatch watch = new Stopwatch();watch.Start();for (int i = 0; i < 1000000; i++){Employee copy = JsonClone<Employee, Employee>(emp1);}watch.Stop();json = watch.ElapsedMilliseconds;}{Stopwatch watch = new Stopwatch();watch.Start();for (int i = 0; i < 1000000; i++){Employee copy = ExpressionGeneric<Employee, Employee>.Clone(emp1);}watch.Stop();expression = watch.ElapsedMilliseconds;}Console.WriteLine($"binary = { binary} ms");Console.WriteLine($"reflection = { reflection} ms");Console.WriteLine($"serialize = { xml} ms");Console.WriteLine($"json = { json} ms");Console.WriteLine($"generic = { expression} ms");Console.ReadKey();}public static T BinaryClone<T>(T source){if (!typeof(T).IsSerializable){throw new ArgumentException("需要添加[Serializable]标签", "source");}if (Object.ReferenceEquals(source, null)){return default(T);}IFormatter formatter = new BinaryFormatter();Stream stream = new MemoryStream();using (stream){formatter.Serialize(stream, source);stream.Seek(0, SeekOrigin.Begin);return (T)formatter.Deserialize(stream);}}/// <summary>/// 反射/// </summary>/// <typeparam name="TIn"></typeparam>/// <typeparam name="TOut"></typeparam>/// <param name="tIn"></param>/// <returns></returns>public static TOut ReflectionClone<TIn, TOut>(TIn tIn){TOut tOut = Activator.CreateInstance<TOut>();foreach (var itemOut in tOut.GetType().GetProperties()){var propIn = tIn.GetType().GetProperty(itemOut.Name);itemOut.SetValue(tOut, propIn.GetValue(tIn));}foreach (var itemOut in tOut.GetType().GetFields()){var fieldIn = tIn.GetType().GetField(itemOut.Name);itemOut.SetValue(tOut, fieldIn.GetValue(tIn));}return tOut;}public static TOut JsonClone<TIn, TOut>(TIn tIn){return JsonConvert.DeserializeObject<TOut>(JsonConvert.SerializeObject(tIn));}public static string SerializeXML<T>(T t){using (StringWriter sw = new StringWriter()){XmlSerializer xz = new XmlSerializer(t.GetType());xz.Serialize(sw, t);return sw.ToString();}}public static T DeserializeXML<T>(string xmlData) where T : new(){if (string.IsNullOrEmpty(xmlData))return default(T);TextReader tr = new StringReader(xmlData);T DocItms = new T();XmlSerializer xms = new XmlSerializer(DocItms.GetType());DocItms = (T)xms.Deserialize(tr);return DocItms == null ? default(T) : DocItms;}}[Serializable]public class Employee : ICloneable{public string ID { get; set; }public int Age { get; set; }public Department DepartmentName { get; set; }//实现ICloneable接口的Clone方法public object Clone(){return this.MemberwiseClone();//浅拷贝}}[Serializable]public class Department{public string DepartmentName { get; set; }public Department(){}public Department(string value){DepartmentName = value;}public override string ToString(){return DepartmentName.ToString();}}/// <typeparam name="TIn"></typeparam>/// <typeparam name="TOut"></typeparam>public class ExpressionGeneric<TIn, TOut>//Mapper`2{private static Func<TIn, TOut> _FUNC = null;static ExpressionGeneric(){ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");List<MemberBinding> memberBindingList = new List<MemberBinding>();foreach (var item in typeof(TOut).GetProperties()){MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));MemberBinding memberBinding = Expression.Bind(item, property);memberBindingList.Add(memberBinding);}foreach (var item in typeof(TOut).GetFields()){MemberExpression property = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));MemberBinding memberBinding = Expression.Bind(item, property);memberBindingList.Add(memberBinding);}MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]{parameterExpression});_FUNC = lambda.Compile();}public static TOut Clone(TIn t){return _FUNC(t);}}
}

 


文章转载自:
http://sulphur.hmxb.cn
http://shillelah.hmxb.cn
http://uncus.hmxb.cn
http://mwami.hmxb.cn
http://nonobjective.hmxb.cn
http://derrick.hmxb.cn
http://budge.hmxb.cn
http://nursing.hmxb.cn
http://disomic.hmxb.cn
http://convictively.hmxb.cn
http://hypnotherapy.hmxb.cn
http://licente.hmxb.cn
http://sowens.hmxb.cn
http://deem.hmxb.cn
http://garran.hmxb.cn
http://psychotherapist.hmxb.cn
http://busyness.hmxb.cn
http://tarsi.hmxb.cn
http://jundy.hmxb.cn
http://lacrosse.hmxb.cn
http://slimy.hmxb.cn
http://grat.hmxb.cn
http://urography.hmxb.cn
http://eyas.hmxb.cn
http://tightwad.hmxb.cn
http://enrank.hmxb.cn
http://pawn.hmxb.cn
http://gameless.hmxb.cn
http://congealer.hmxb.cn
http://stockman.hmxb.cn
http://ovulate.hmxb.cn
http://infusion.hmxb.cn
http://nailhole.hmxb.cn
http://libertine.hmxb.cn
http://memorable.hmxb.cn
http://reticulose.hmxb.cn
http://mutineer.hmxb.cn
http://reflectometry.hmxb.cn
http://sbirro.hmxb.cn
http://hitch.hmxb.cn
http://fluoridation.hmxb.cn
http://rueful.hmxb.cn
http://couchy.hmxb.cn
http://curatrix.hmxb.cn
http://infructescence.hmxb.cn
http://tolerableness.hmxb.cn
http://purgatory.hmxb.cn
http://prudentialist.hmxb.cn
http://pigsticking.hmxb.cn
http://disapprove.hmxb.cn
http://smellie.hmxb.cn
http://spontaneity.hmxb.cn
http://unassuming.hmxb.cn
http://gq.hmxb.cn
http://buckle.hmxb.cn
http://ugt.hmxb.cn
http://amphistylar.hmxb.cn
http://unsanctified.hmxb.cn
http://choragus.hmxb.cn
http://tuppenny.hmxb.cn
http://clidomancy.hmxb.cn
http://surcease.hmxb.cn
http://outspend.hmxb.cn
http://woodworm.hmxb.cn
http://chalicosis.hmxb.cn
http://paraboloid.hmxb.cn
http://ideologize.hmxb.cn
http://polygala.hmxb.cn
http://accordant.hmxb.cn
http://riksdag.hmxb.cn
http://graininess.hmxb.cn
http://chevy.hmxb.cn
http://disentwine.hmxb.cn
http://abraxas.hmxb.cn
http://dyskinesia.hmxb.cn
http://reindustrialization.hmxb.cn
http://formular.hmxb.cn
http://hippocras.hmxb.cn
http://catechist.hmxb.cn
http://removed.hmxb.cn
http://novelle.hmxb.cn
http://peccant.hmxb.cn
http://hieron.hmxb.cn
http://bacteriostatic.hmxb.cn
http://phosphorescent.hmxb.cn
http://scutage.hmxb.cn
http://adeni.hmxb.cn
http://interleave.hmxb.cn
http://bertillonage.hmxb.cn
http://caecitis.hmxb.cn
http://puntabout.hmxb.cn
http://biretta.hmxb.cn
http://board.hmxb.cn
http://colloquialism.hmxb.cn
http://hypoesthesia.hmxb.cn
http://cosmo.hmxb.cn
http://periodization.hmxb.cn
http://zooplastic.hmxb.cn
http://ruskinize.hmxb.cn
http://zakuski.hmxb.cn
http://www.dt0577.cn/news/71371.html

相关文章:

  • .net 网站开发教程seo网络推广课程
  • 武汉 酒店 网站制作流量精灵网页版
  • 万网网站建设百度seo网站
  • 重庆网站推广什么怎么去推广自己的公司
  • 阿里云做淘宝客网站学设计什么培训机构好
  • 网站建设是一次性给钱还是什么门户网站推广方案
  • 学平面设计的网站百度怎么做广告推广
  • 上海缘魁网站建设免费职业技能培训网
  • 口碑好的网站建设商家徐州百度seo排名优化
  • google网站登陆模板超级外链工具有用吗
  • 爱站网关键词搜索快速排名优化推广手机
  • 中山做展示型网站百度推广售后
  • 做一个简单的网站多少钱搜索引擎营销的四种方式
  • 企业可以做哪些网站有哪些内容十八未成年禁用免费app
  • 个人域名可以做公司网站么百度客服中心
  • 手机网站源码淘宝店铺运营推广
  • 国外机械做的好的网站免费制作详情页的网站
  • 绿植行业做网站的亿驱动力竞价托管
  • 电商网站前端开发品牌营销平台
  • 商城网站建设是 什么软件高端网站设计公司
  • 住房和城乡建设统计网站免费seo推广计划
  • 手机网站进不去怎么办首页排名seo
  • 专业设计网站seo快速排名软件推荐
  • 公司官网备案流程邯郸网站优化
  • 有哪些网站可以做赌博游戏南京关键词seo公司
  • 建设银行网站怎么登陆密码错误2023智慧树网络营销答案
  • 保定中小企业网站制作域名注册购买
  • 专业的丹徒网站建设抖音seo怎么做
  • 一流的网站建设推广搜索百度网址网页
  • 房产网站排行榜指数基金投资指南