如何实现ICloneable接口的Clone方法?

解决方案 »

  1.   

    示例:
      [Serializable]
        public class Test : ICloneable
        {    
            public string abc;
    public string add;
     
            public object Clone()
            {
                MemoryStream ms = new MemoryStream();
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, this);
                ms.Position = 0;
                object obj = bf.Deserialize(ms);
                ms.Close();
                return obj;
            }    }
    //....
    Test test1= new Test();
    test1.abc="zhang";
    test1.add="jason";
    Test test2 = (Test) test1.Clone();
      

  2.   

    谢谢'大康',用户控件是要从System.Windows.Forms.UserControl继承的,System.Windows.Forms.UserControl是不可序列化的。所是你提供的方法还是不行。