首页 > 代码库 > C#_deepCopy
C#_deepCopy
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime.Serialization.Formatters.Binary;using System.Text;using System.Threading.Tasks;using System.Windows;namespace ConsoleApplication1{ [Serializable] class Person { public Person() { } public int age { set; get; } public string name { set; get; } } class Program { public static Person deepCopy(Person value) { using (MemoryStream stream = new MemoryStream()) { BinaryFormatter formattor = new BinaryFormatter(); formattor.Serialize(stream,value); stream.Flush(); stream.Seek(0,SeekOrigin.Begin); return (Person)formattor.Deserialize(stream); } } static void Main(string[] args) { Person p1 = new Person(); p1.age=20; p1.name = "aaa"; Person p2 = new Person(); p2.age = 20; p2.name = "aaa"; //Console.WriteLine(p1==p2); //Console.WriteLine(p1.Equals(p2)); Person p3 = deepCopy(p2); p2.age = 30; Console.WriteLine(p2.age); Console.WriteLine(p3.age); Console.ReadKey(); } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。