日期:2014-05-18  浏览次数:20806 次

深拷贝的问题。
哪位高手给个例子代码呢
从定义到应用

谢谢!

Have you ever used the Clone() method of DataSet? This method creates an empty class with same structure as original DataSet.

 

You can write your own clonable classes. To do so, you must implement IClonable. The following code shows a clonable Test class. 

 

using System.IO;

using System.Runtime.Serialization.Formatters.Binary;

C# code

public Class Test : IClonable

{

    public Test()

    {

    }

    // deep copy in separeate memory space

    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;

    }

}



但是实现出来的Clone方法之前并不能加public关键字,既然是这样,那么怎么调用呢?
用实现出来的ICloneable接口的实例来吧,Clone又不能给出来参数。
郁闷

------解决方案--------------------
Test a = new Test();
Test b = (Test) a.Clone();
是b指向了a clone出来的对象,a的clone方法把自己内部的状态信息序列化,然后反序列化到新的Object中,你是不是想知道这个?
------解决方案--------------------
请参考
http://www.cnblogs.com/Phoenix-Rock/archive/2006/11/07/shallowcopy_deepcopy.html
------解决方案--------------------
C# code

[Serializable]
public Class Test : IClonable

{

    public Test()

    {

    }

    // deep copy in separeate memory space

    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;

    }

}

------解决方案--------------------
C# code


[Serializable]
    public class UserInfo
    {
        private int m_ID;
        private string m_FirstName;
        private string m_LastName;
        private string m_Email;
        private string m_HomePhone;
        public string[] m_strArr;
        public UserInfo()
        {
            m_ID = 0;
            m_FirstName = "";
            m_LastName = "";
            m_Email = "";
            m_HomePhone = "";
            m_strArr = new string[10] { "abc1", "abc2", "abc3", "abc4", "abc5", "abc6", "abc7", "abc8", "abc9", "abc10" };
        }
        public object Clone()
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream memStream = new MemoryStream();
            formatter.Serialize(memStream, this);
            memStream.Position = 0;
            object obj = formatter.Deserialize(memStream);
            memStream.Close();

            return obj;
        }
        public int ID
        {
            get { return m_ID; }
            set { m_ID = value; }
        }
        public string Email
        {
            get { return m_Email; }
            set { m_Email = value; }
        }
        public string LastName
        {
            get { return m_LastName; }
            set { m_LastName = value; }
        }
        public string FirstName
        {
            get { return m_FirstName; }
            set { m_FirstName = value; }
        }
        public string HomePhone
        {
            get { return m_HomePhone; }
            set { m_HomePhone = value; }
        }

    }
//test:
 UserInfo info = new UserInfo();
 UserInfo tmp = (UserInfo)info.Clone();

------解决方案--------------------