深度拷贝。浅层拷贝。先手写浅层拷贝、深层拷贝的代码,理解了概念再谈用MemberwiseClone 、DeepCopy。(面试题)
写代码拷贝,实现ICloneable方法,内部调用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);
}
}
这个要怎么理解 序列化之后然后连接内部拷贝? |