码迷,mamicode.com
首页 > Windows程序 > 详细

关于C#类对象克隆问题: 浅克隆和深克隆

时间:2016-06-16 20:06:10      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

运行下面一段代码,就知道了:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.Linq;
  5 using System.Runtime.Serialization.Formatters.Binary;
  6 using System.Text;
  7 
  8 namespace DeepCloneProj
  9 {
 10     class Program
 11     {
 12         static void Main(string[] args)
 13         {
 14             A originalInstance = new A(1, "Norton");
 15             originalInstance.A3.Add(2);
 16             originalInstance.A3.Add(3);
 17 
 18             WriteData("[SOURCE]", originalInstance);
 19 
 20             Console.WriteLine("-----------------BEFORE-------------------------");
 21 
 22             A shallowClonedInstance = originalInstance.Clone() as A;
 23             WriteData("[SHALLOW CLONED]", shallowClonedInstance);
 24 
 25             Console.WriteLine("-----------------BEFORE-------------------------");
 26 
 27             A deepClonedInstance = originalInstance.DeepClone() as A;
 28             WriteData("[DEEP CLONED]", deepClonedInstance);
 29 
 30             Console.WriteLine("-----------------BEFORE-------------------------");
 31             Console.WriteLine();
 32             Console.WriteLine("Change the A2 value: originalInstance.A2=\"FlexWorkflow\"");
 33             originalInstance.A2 = "FlexWorkflow";
 34             Console.WriteLine("Add a new value for originalInstance.A3: originalInstance.A3.Add(100)");
 35             Console.WriteLine();
 36             originalInstance.A3.Add(100);
 37 
 38             Console.WriteLine("-----------------AFTER-------------------------");
 39 
 40             WriteData("[SOURCE]", originalInstance);
 41 
 42             Console.WriteLine("-----------------AFTER-------------------------");
 43 
 44             WriteData("[SHALLOW CLONED]", shallowClonedInstance);
 45 
 46             Console.WriteLine("-----------------AFTER-------------------------");
 47 
 48             WriteData("[DEEP CLONED]", deepClonedInstance);
 49 
 50             Console.Read();
 51 
 52         }
 53 
 54         private static void WriteData(string source, A originalInstance)
 55         {
 56             Console.WriteLine("Instance:{0}", source);
 57             Console.WriteLine("originalInstace.A1:{0}", originalInstance.A1);
 58             Console.WriteLine("originalInstace.A2:{0}", originalInstance.A2);
 59             Console.WriteLine("originalInstace.A3:");
 60             foreach (int value in originalInstance.A3)
 61             {
 62                 Console.WriteLine(value);
 63             }
 64         }
 65     }
 66 
 67     [Serializable]
 68     public class A : ICloneable
 69     {
 70         private int _a1;
 71         private string _a2;
 72         private IList<int> _a3;
 73 
 74         public A(int a1, string a2)
 75         {
 76             this._a1 = a1;
 77             this._a2 = a2;
 78             this._a3 = new List<int>();
 79         }
 80 
 81         public int A1
 82         {
 83             get
 84             {
 85                 return this._a1;
 86             }
 87         }
 88 
 89         public string A2
 90         {
 91             get
 92             {
 93                 return this._a2;
 94             }
 95             set
 96             {
 97                 this._a2 = value;
 98             }
 99         }
100 
101         public IList<int> A3
102         {
103             get
104             {
105                 return this._a3;
106             }
107         }
108 
109         public object DeepClone()
110         {
111             object clonedInstance = null;
112             using (MemoryStream stream = new MemoryStream())
113             {
114                 try
115                 {
116                     BinaryFormatter formatter = new BinaryFormatter();
117                     formatter.Serialize(stream, this);
118                     stream.Position = 0;
119                     clonedInstance = formatter.Deserialize(stream);
120                 }
121                 catch
122                 {
123                     throw;
124                 }
125             }
126 
127             return clonedInstance;
128         }
129 
130         public object Clone()
131         {
132             return MemberwiseClone();
133         }
134     }
135 }

 运行结果:

Instance:[SOURCE]
originalInstace.A1:1
originalInstace.A2:Norton
originalInstace.A3:
2
3
-----------------BEFORE-------------------------
Instance:[SHALLOW CLONED]
originalInstace.A1:1
originalInstace.A2:Norton
originalInstace.A3:
2
3
-----------------BEFORE-------------------------
Instance:[DEEP CLONED]
originalInstace.A1:1
originalInstace.A2:Norton
originalInstace.A3:
2
3
-----------------BEFORE-------------------------

Change the A2 value: originalInstance.A2="FlexWorkflow"
Add a new value for originalInstance.A3: originalInstance.A3.Add(100)

-----------------AFTER-------------------------
Instance:[SOURCE]
originalInstace.A1:1
originalInstace.A2:FlexWorkflow
originalInstace.A3:
2
3
100
-----------------AFTER-------------------------
Instance:[SHALLOW CLONED]
originalInstace.A1:1
originalInstace.A2:Norton
originalInstace.A3:
2
3
100
-----------------AFTER-------------------------
Instance:[DEEP CLONED]
originalInstace.A1:1
originalInstace.A2:Norton
originalInstace.A3:
2
3

 

关于C#类对象克隆问题: 浅克隆和深克隆

标签:

原文地址:http://www.cnblogs.com/Min-min/p/5591806.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!