深拷贝-浅拷贝
深拷贝-浅拷贝的概念就不说了,基础的东西! 实现如下:
public class Person { public string Name { get; set; } public Car Car { get; set; } public Person ShallowClone() { //一句话实现浅拷贝。Object的方法 return this.MemberwiseClone() as Person; //MemberwiseClone的作用就是把当前对象浅拷贝一份。 } //通过序列化与反序列化会将当前对象实现一次深拷贝 public Person DeepClone() { //类需要Serializable BinaryFormatter bf = new BinaryFormatter(); using (MemoryStream ms = new MemoryStream()) { bf.Serialize(ms, this); ms.Position = 0; return bf.Deserialize(ms) as Person; } } } public class Car { public string Color { get; set; } }