首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

深拷贝的有关问题

2012-03-01 
深拷贝的问题。哪位高手给个例子代码呢从定义到应用谢谢!Have you ever used the Clone() method of DataSe

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

谢谢!

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();
------解决方案--------------------


你的最初的例子ICloneable写少了一个e,另外你的类必须要可以被序列化,因为你调用了
formatter.Serialize(memStream, this);
所以要加一个Serializable标签,下面的代码可以运行,试试

C# code
using System;using System.Collections.Generic;using System.Text;using System.IO;using System.Runtime.Serialization.Formatters.Binary;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            Test testA = new Test("CSDN");            Test testB = (Test) testA.Clone();            Console.WriteLine(testB.Inf);            Contract objA = new Contract();            objA.ContractName = "CSDN Contract";            Contract objB = (Contract)((ICloneable)objA).Clone();            Console.WriteLine(objB.ContractName);        }    }    [Serializable]    public class Test : ICloneable    {        public Test(string inf)        {            this.m_inf = inf;         }        private string m_inf;        public string Inf        {            get { return m_inf; }            set { m_inf = value; }        }        // 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;        }    }    [Serializable]    public class Contract : ICloneable    {        private string m_contractName = string.Empty;        private int? m_contractId = 0;        /// <summary>        ///         /// </summary>        public string ContractName        {            get { return m_contractName; }            set { m_contractName = value; }        }        /// <summary>        ///         /// </summary>        public int? ContractID        {            get { return m_contractId; }            set { m_contractId = value; }        }        /// <summary>        ///         /// </summary>        /// <returns></returns>        object ICloneable.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;        }    }} 

热点排行