码迷,mamicode.com
首页 > 其他好文 > 详细

深copy

时间:2016-04-07 20:38:24      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

更好的对一个对象进行复制

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using ChartUtil;
 6 using System.Windows.Forms;
 7 using System.IO;
 8 using System.Runtime.Serialization;
 9 using System.Runtime.Serialization.Formatters.Binary;
10 //using System.Runtime.Serialization.Formatters.Soap;
11 
12 namespace NetAnalysis.Common
13 {
14     public static class DrawChartExtensionMethodClass
15     {
16 
17 
18         #region 单击事件
19         public static void ChartMouseRigthClick(this DrawChart dict)
20         {
21 
22             dict.ChartMouseClick += delegate
23             {
24 
25                 MessageBox.Show("kkk");
26             };
27         }
28 
29 
30 
31 
32         #endregion
33     }
34 
35     [Serializable]
36     public class TimeRegion : ICloneable
37     {
38 
39         public List<List<DateTime>> timeregion = new List<List<DateTime>>();
40 
41 
42         #region ICloneable 成员
43 
44         public object Clone()
45         {
46         //  return this.MemberwiseClone();
47             return new TimeRegion() as TimeRegion ;
48         }
49 
50         #endregion
51 
52         public TimeRegion DeepClone()
53         {
54             using (Stream objectStream = new MemoryStream())
55             {
56                 IFormatter formatter = new BinaryFormatter();
57                 //IFormatter formatter = new SoapFormatter();
58                 formatter.Serialize(objectStream, this);
59                 objectStream.Seek(0, SeekOrigin.Begin);
60                 return formatter.Deserialize(objectStream) as TimeRegion;
61             }
62 
63             ////这个版本有问题
64             //TimeRegion rtd = new TimeRegion();
65             //rtd.timeregion = this.timeregion;
66 
67             //return rtd;
68         }
69 
70         public TimeRegion ShallowClone()
71         {
72             return Clone() as TimeRegion;
73         }
74 
75 
76 
77     }
78 }

 

深copy

标签:

原文地址:http://www.cnblogs.com/billly/p/5365007.html

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