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; namespace 深拷贝浅拷贝_对象序列化_ { class Program { static void Main(string[] args) { #region 深浅拷贝原理 ////拷贝:数据的复制 ////Person p1 = new Person() { Age = 19, Name = "soso", Sex = "男", MyCar = new Car() { Color = "Green" } }; //////浅拷贝: ////Person p2 = new Person(); ////p2.Age = 23; ////p2.Name = "jk"; ////p2.Sex = "女"; ////p2.MyCar = p1.MyCar; ////Car c1 = p1.MyCar; ////Car c2 = p2.MyCar; ////深拷贝 //Person p1 = new Person(); //p1.Age = 13; //p1.MyCar = new Car() { Color = "Black" }; //Person p2 = new Person(); //p2.Age = p1.Age; //p2.MyCar = new Car() { Color = "Black" }; //p2.MyCar = new Car() { Color = p1.MyCar.Color }; ////p2.MyCar = new Car() { Color = "Black" }; //// &c1 ////0x0694e990 //// c1: 0x027f1378 ////&c2 ////0x0694e98c //// c2: 0x027f13a4 //Car c1 = p1.MyCar; //Car c2 = p2.MyCar; ////p2.MyCar = new Car() { Color = p1.MyCar.Color }; //// &c1 ////0x0641ec70 //// c1: 0x0214b378 ////&c2 ////0x0641ec6c //// c2: 0x0214b3a4 #endregion //编程实现深拷贝和浅拷贝 //浅拷贝 //Copy copy1 = new Copy(); //copy1.State = "Shallow"; //copy1.Per = new Person() { Age = 19, MyCar = new Car() { Color = "Blue" } }; //Copy copy2 = copy1.ShallowCopy(); //Person per1 = copy1.Per; //Person per2 = copy2.Per; //Console.WriteLine(copy1.Per.Age + "|" + copy1.Per.MyCar.Color + "|" + copy1.State); //Console.WriteLine(copy2.Per.Age + "|" + copy2.Per.MyCar.Color + "|" + copy2.State); /* * 19|Blue|Shallow * 19|Blue|Shallow &per1 0x0648ea70 per1: 0x02569388 &per2 0x0648ea6c per2: 0x02569388 * */ //深拷贝1, //Copy copy11 = new Copy() { Per = new Person() { Name = "DeepCopy", MyCar = new Car() { Color = "DarkBlack" } } }; //copy11.State = "Deep"; //Copy copy22 = copy11.DeepCopy1(copy11); //Console.WriteLine(copy11.State + "|" + copy11.Per.Name + "|" + copy11.Per.MyCar.Color); //Console.WriteLine(copy22.State + "|" + copy22.Per.Name + "|" + copy22.Per.MyCar.Color); //Car c1 = copy11.Per.MyCar; //Car c2 = copy22.Per.MyCar; /* * Deep|DeepCopy|DarkBlack * Deep|DeepCopy|DarkBlack &c1 0x0584e8f0 c1: 0x022d13d8 &c2 0x0584e8ec c2: 0x022d1414 * */ //深拷贝2. Copy cp = new Copy(); cp.State = "Deep2"; cp.Per = new Person() { Age = 19 }; cp.Per.MyCar = new Car() { Price = 1900 }; Copy cp2 = cp.DeepCopy(); Console.WriteLine(cp.State + "|" + cp.Per.Age + "|" + cp.Per.MyCar.Price); Console.WriteLine(cp2.State + "|" + cp2.Per.Age + "|" + cp2.Per.MyCar.Price); Person pp = cp.Per; Person ppp = cp2.Per; /* Deep2|19|1900 Deep2|19|1900 &pp 0x0598ed00 pp: 0x024cb380 &ppp 0x0598ecfc ppp: 0x024d0acc */ Console.ReadKey(); } } [Serializable] class Copy { private string state; public string State { get { return state; } set { state = value; } } private Person per; public Person Per { get { return per; } set { per = value; } } public Copy ShallowCopy() { return (Copy)this.MemberwiseClone(); } /// <summary> /// 实现这个方法要加序列化标记 /// </summary> public Copy DeepCopy() { BinaryFormatter bf = new BinaryFormatter(); using (MemoryStream ms = new MemoryStream()) { //程序集“深拷贝浅拷贝[对象序列化], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中的类型“深拷贝浅拷贝_对象序列化_.Copy”未标记为可序列化。 //程序集“深拷贝浅拷贝[对象序列化], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中的类型“深拷贝浅拷贝_对象序列化_.Person”未标记为可序列化。 //程序集“深拷贝浅拷贝[对象序列化], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中的类型“深拷贝浅拷贝_对象序列化_.Car”未标记为可序列化。 bf.Serialize(ms, this); ms.Position = 0; return (Copy)bf.Deserialize(ms); } } internal Copy DeepCopy1(Copy copy) { //Copy c = (Copy)this.MemberwiseClone(); Copy c = copy.ShallowCopy(); c.Per = new Person() { MyCar = new Car() { Color = c.per.MyCar.Color }, Name = c.per.Name }; return c; } } [Serializable] class Person { private Car myCar; public Car MyCar { get { return myCar; } set { myCar = value; } } /// <summary> /// 年龄 /// </summary> private int age; /// <summary> /// 年龄 /// </summary> public int Age { get { return age; } set { age = value; } } /// <summary> /// 名字 /// </summary> private string name; /// <summary> /// 名字 /// </summary> public string Name { get { return name; } set { name = value; } } /// <summary> /// 性别 /// </summary> private string sex; /// <summary> /// 性别 /// </summary> public string Sex { get { return sex; } set { sex = value; } } } [Serializable] class Car { /// <summary> /// 车名 /// </summary> private string name; /// <summary> /// 车名 /// </summary> public string Name { get { return name; } set { name = value; } } /// <summary> /// 颜色 /// </summary> private string color; /// <summary> /// 颜色 /// </summary> public string Color { get { return color; } set { color = value; } } /// <summary> /// 价格 /// </summary> private int price; /// <summary> /// 价格 /// </summary> public int Price { get { return price; } set { price = value; } } } }
原文:http://www.cnblogs.com/wjshan0808/p/3521747.html