浅度复制(MemberwiseClone)
演示代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace EventDemo { public class RefPoint { // 定义一个引用类型 public int x; public RefPoint(int x) { this.x = x; } } public struct ValPoint { // 定义一个值类型 public int x; public ValPoint(int x) { this.x = x; } } // 将要进行 浅度复制 的对象,注意为 引用类型 public class RefLine : ICloneable { public RefPoint rPoint; public ValPoint vPoint; public RefLine(RefPoint rPoint, ValPoint vPoint) { this.rPoint = rPoint; this.vPoint = vPoint; } public object Clone() { return MemberwiseClone(); } } class Program { static void Main(string[] args) { RefPoint rPoint = new RefPoint(1); ValPoint vPoint = new ValPoint(1); RefLine line = new RefLine(rPoint, vPoint); RefLine newLine = (RefLine)line.Clone(); Console.WriteLine("Original: line.rPoint.x = {0}, line.vPoint.x = {1}", line.rPoint.x, line.vPoint.x); Console.WriteLine("Cloned: newLine.rPoint.x = {0}, newLine.vPoint.x = {1}", newLine.rPoint.x, newLine.vPoint.x); line.rPoint.x = 10; // 修改原先的line的 引用类型成员 rPoint line.vPoint.x = 10; // 修改原先的line的 值类型 成员 vPoint Console.WriteLine("Original: line.rPoint.x = {0}, line.vPoint.x = {1}", line.rPoint.x, line.vPoint.x); Console.WriteLine("Cloned: newLine.rPoint.x = {0}, newLine.vPoint.x = {1}", newLine.rPoint.x, newLine.vPoint.x); Console.ReadLine(); } } }
深度复制
演示代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace EventDemo { public class RefPoint { // 定义一个引用类型 public int x; public RefPoint(int x) { this.x = x; } } public struct ValPoint { // 定义一个值类型 public int x; public ValPoint(int x) { this.x = x; } } // 将要进行 浅度复制 的对象,注意为 引用类型 public class RefLine : ICloneable { public RefPoint rPoint; public ValPoint vPoint; public RefLine(RefPoint rPoint, ValPoint vPoint) { this.rPoint = rPoint; this.vPoint = vPoint; } public object Clone() { RefPoint rPoint = new RefPoint(this.rPoint.x); // 对于引用类型,创建新对象 ValPoint vPoint = this.vPoint; // 值类型,直接赋值 RefLine newLine = new RefLine(rPoint, vPoint); return newLine; } } class Program { static void Main(string[] args) { RefPoint rPoint = new RefPoint(1); ValPoint vPoint = new ValPoint(1); RefLine line = new RefLine(rPoint, vPoint); RefLine newLine = (RefLine)line.Clone(); Console.WriteLine("Original: line.rPoint.x = {0}, line.vPoint.x = {1}", line.rPoint.x, line.vPoint.x); Console.WriteLine("Cloned: newLine.rPoint.x = {0}, newLine.vPoint.x = {1}", newLine.rPoint.x, newLine.vPoint.x); line.rPoint.x = 10; // 修改原先的line的 引用类型成员 rPoint line.vPoint.x = 10; // 修改原先的line的 值类型 成员 vPoint Console.WriteLine("Original: line.rPoint.x = {0}, line.vPoint.x = {1}", line.rPoint.x, line.vPoint.x); Console.WriteLine("Cloned: newLine.rPoint.x = {0}, newLine.vPoint.x = {1}", newLine.rPoint.x, newLine.vPoint.x); Console.ReadLine(); } } }
深度复制(序列化方式)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization.Formatters.Binary; using System.IO; namespace EventDemo { [Serializable()] public class RefPoint { // 定义一个引用类型 public int x; public RefPoint(int x) { this.x = x; } } [Serializable()] public struct ValPoint { // 定义一个值类型 public int x; public ValPoint(int x) { this.x = x; } } // 将要进行 浅度复制 的对象,注意为 引用类型 [Serializable()] public class RefLine : ICloneable { public RefPoint rPoint; public ValPoint vPoint; public RefLine(RefPoint rPoint, ValPoint vPoint) { this.rPoint = rPoint; this.vPoint = vPoint; } public object Clone() { BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, this); ms.Position = 0; return (bf.Deserialize(ms)); ; } } class Program { static void Main(string[] args) { RefPoint rPoint = new RefPoint(1); ValPoint vPoint = new ValPoint(1); RefLine line = new RefLine(rPoint, vPoint); RefLine newLine = (RefLine)line.Clone(); Console.WriteLine("Original: line.rPoint.x = {0}, line.vPoint.x = {1}", line.rPoint.x, line.vPoint.x); Console.WriteLine("Cloned: newLine.rPoint.x = {0}, newLine.vPoint.x = {1}", newLine.rPoint.x, newLine.vPoint.x); line.rPoint.x = 10; // 修改原先的line的 引用类型成员 rPoint line.vPoint.x = 10; // 修改原先的line的 值类型 成员 vPoint Console.WriteLine("Original: line.rPoint.x = {0}, line.vPoint.x = {1}", line.rPoint.x, line.vPoint.x); Console.WriteLine("Cloned: newLine.rPoint.x = {0}, newLine.vPoint.x = {1}", newLine.rPoint.x, newLine.vPoint.x); Console.ReadLine(); } } }