c# 對象的深拷備

來源:互聯網
上載者:User

標籤:tar   16px   system   new   setvalue   htm   copy   public   type   

C# 參考型別對象在拷備時,一般有淺拷備與深拷備,淺拷備指向的是同一對象的引用地址,一個對象的修改必然會影響另一個對象,而深拷備是直接拷備對象的內容,而不是引用,拷備後的對象擁有新的引用,這裡主要介紹深拷備的實現,其實現 方式有3種,好了直接上代碼:

1. 首先定義一個類:

using System;using System.IO;namespace DemonCol{    [Serializable]  // 可序列化標記   public  class Person    {        public int Age { get; set; }        public string Name { get; set; }        public  FileMode  MyFileMode { get; set; }    }}

 2. 定義深拷備方法

     方法1: 通過序列化與還原序列化實現深拷備

    需要添加的引用: using System.Runtime.Serialization.Formatters.Binary;  using System.IO; using System.Runtime.Serialization;

    Person 類的頭部必須添加標記:  [Serializable] 

   

        /// <summary>        ///  通過序列化與還原序列化實現深拷備         /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="obj"></param>        /// <returns></returns>        public static T DeepCopyBySerialize<T>(T obj)        {            object returnObj;            using (MemoryStream ms = new MemoryStream())            {                IFormatter formatter = new BinaryFormatter();                formatter.Serialize(ms, obj);                ms.Seek(0, SeekOrigin.Begin);                returnObj = formatter.Deserialize(ms);                ms.Close();            }            return (T)returnObj;        }

 

方法2:  通過 XML  序列化與還原序列化實現 深拷備

需要添加的引用: using System.Xml.Serialization;

Person 類的頭部可不用 標記:[Serializable] 

        /// <summary>        /// 通過 XML  序列化與還原序列化實現 深拷備        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="obj"></param>        /// <returns></returns>        public static T DeepCopyByXmlSerialize<T>(T obj)        {            object returnObj;            using (MemoryStream ms = new MemoryStream())            {                XmlSerializer xs = new XmlSerializer(typeof(T));                xs.Serialize(ms, obj);                ms.Seek(0, SeekOrigin.Begin);                returnObj = xs.Deserialize(ms);                ms.Close();            }            return (T)returnObj;        }

 

方法3:  通過 反射實現 深拷備

需要添加的引用: using System.Reflection;

Person 類的頭部 可不用 標記:[Serializable] 

         /// <summary>        /// 通過 反射實現 深拷備        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="obj"></param>        /// <returns></returns>        public static T DeepCopyByReflection<T>(T obj)        {            if (obj is string || obj.GetType().IsValueType)                return obj;            object returnObj = Activator.CreateInstance(obj.GetType());            FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);            foreach (FieldInfo item in fields)            {                try                {                    item.SetValue(returnObj, DeepCopyByReflection(item.GetValue(obj)));                }                catch { }            }            return (T)returnObj;        }

 

3. 用戶端測試

        static void Main(string[] args)        {            Person person = new Person();            person.Age = 20;            person.Name = "will";            person.MyFileMode = FileMode.Create;            Person person2 = person;            Person person3 = DeepCopyBySerialize<Person>(person);            person.Name = "modify";            person.Age = 10;            person.MyFileMode = FileMode.Open;
   Console.ReadKey(); }

  運行結果:

 

 

 

參考文章: http://www.jb51.net/article/67992.htm

 

c# 對象的深拷備

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.