c# 對象的深拷備
阿新 • • 發佈:2017-07-01
mes blank typeof person 技術分享 using flags format inf
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# 對象的深拷備