浅表拷贝:得到一个与原始对象类型、值相同的新实例。如果字段是引用类型,该引用被拷贝,而不是拷贝引用的对象。
深层拷贝:包含对象直接或间接引用的对象的所有拷贝。
浅拷贝:
实现ICloneable接口
class testClone { public int testint { get; set; } public string teststr { get; set; } public Object Clone() { return (Object)this.MemberwiseClone(); } }
深拷贝:
static object DeepCopy(object src) { BinaryFormatter formatter = new BinaryFormatter(null,new StreamingContext(StreamingContextStates.Clone)); using (MemoryStream stream=new MemoryStream()) { formatter.Serialize(stream, src); stream.Position = 0; return formatter.Deserialize(stream); } }