我试过Clone(),但得到的是浅表副本

解决方案 »

  1.   

    http://www.codeproject.com/csharp/cloneimpl_class.asp#xx377851xxHi,just wondering why you don't use the .NET serialization for the purpose of cloning an object. The advantage is -IMHO- that you have more control of what should be cloned and what not (simply  a field with the NonSerialized attribute).example:[Serializable]class Foo: ICloneable{    [NonSerialized]    public int TempVar;    public string Text;    public object Clone()    {        Stream stream=new MemoryStream();        try        {            BinaryFormatter bf = new BinaryFormatter();            bf.Serialize(stream, this);                    stream.Position=0;                    return bf.Deserialize(stream);        }        finally        {            stream.Close();        }    }}
    The Clone method is always the same and could therefore be placed in a utility class.
    Just a thought...
      

  2.   

    先谢谢brightheroes(闭关|那一剑的风情)。
    以前用过类似序列化的方法,将对象存成Xml然后再取回来,
    现在用的是重写Clone() 觉得好麻烦。
    C#中是不是没有提供深层Copy的方法?