C# ICloneable,shallow clone,deep clone.

 1 [Serializable]
 2     public class Person:ICloneable
 3     {       
 4         public string Name { get; set; }
 5         public int Id { get; set; }
 6         public int Age { get; set; }
 7         public Department Department { get; set; }
 8 
 9         public object Clone()
10         {
11            return  this.MemberwiseClone();
12         }
13 
14 
15         public Person ShallowClone()
16         {
17             return Clone() as Person;
18         }
19 
20         public Person DeepClone()
21         {
22             using (Stream objStream = new MemoryStream())
23             {
24                 IFormatter formatter = new BinaryFormatter();
25                 formatter.Serialize(objStream, this);
26                 objStream.Seek(0, SeekOrigin.Begin);
27                 return formatter.Deserialize(objStream) as Person;
28             }
29         }
30     }
31 
32     [Serializable]
33     public class Department
34     {
35         public string DeptName { get; set; }
36         public int DeptId { get; set; }
37     }

 



 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             CloneDemo();
 6             Console.ReadLine();
 7         }
 8 
 9         static void CloneDemo()
10         {
11             Person fred = new Person()
12             {
13                 Id = 1,
14                 Name = "Fred",
15                 Age = 32,
16                 Department=new Department()
17                 {
18                     DeptId=1,
19                     DeptName="Dept1"
20                 }
21             };
22 
23             Console.WriteLine($"fred,name:{fred.Name},Id:{fred.Name},department name:{fred.Department.DeptName},deptid:{fred.Department.DeptId}");
24 
25             Person shallowClonePerson = fred.ShallowClone() as Person;
26             Console.WriteLine($"shallowClonePerson,Name:{shallowClonePerson.Name},Id:{shallowClonePerson.Id},department name:{shallowClonePerson.Department.DeptName},deptId:{shallowClonePerson.Department.DeptId}");
27 
28             fred.Id = 2;
29             fred.Name = "fred2";
30             fred.Age = 33;
31             fred.Department.DeptId = 2;
32             fred.Department.DeptName = "dept2";
33             Console.WriteLine($"shallowClonePerson,Name:{shallowClonePerson.Name},Id:{shallowClonePerson.Id},department name:{shallowClonePerson.Department.DeptName},deptId:{shallowClonePerson.Department.DeptId}");
34 
35 
36             Person deepClonePerson = fred.DeepClone() as Person;                      
37             Console.WriteLine($"deepClonePerson,Name:{deepClonePerson.Name},Id:{deepClonePerson.Id},department name:{deepClonePerson.Department.DeptName},id:{deepClonePerson.Department.DeptId}");
38                         
39             fred.Id = 3;
40             fred.Name = "Fred3";
41             fred.Age = 34;
42             fred.Department.DeptId = 3;
43             fred.Department.DeptName = "dept3";
44             Console.WriteLine($"deepClonePerson,Name:{deepClonePerson.Name},Id:{deepClonePerson.Id},department name:{deepClonePerson.Department.DeptName},id:{deepClonePerson.Department.DeptId}");
45         }
46 }

 

你可能感兴趣的:(C# ICloneable,shallow clone,deep clone.)