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

利用序列化和反序列化实现深拷贝

时间:2018-05-23 14:24:13      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:epc   bsp   color   反序列化   bin   binary   orm   nbsp   des   

以下都是有缺陷的。

 

技术分享图片
 1 private static T BinDeepCopy<T>(T t)  
 2       {  
 3           using(var ms = new MemoryStream()) {  
 4               var bf = new BinaryFormatter();  
 5               bf.Serialize(ms,t);  
 6               ms.Position = 0;  
 7               return (T)bf.Deserialize(ms);  
 8           }  
 9       }  
10   
11       private static T DomDeepCopy<T>(T t)  
12       {  
13           using(var ms = new MemoryStream()) {  
14               XmlSerializer xml = new XmlSerializer(typeof(T));  
15               xml.Serialize(ms,t);  
16               ms.Position = 0;  
17               return (T)xml.Deserialize(ms);  
18           }  
19       }  
20 
21 private static T SoapDeepCopy<T>(T t)  
22 {  
23     using(var ms = new MemoryStream()) {  
24         var soap = new SoapFormatter();  
25         soap.Serialize(ms,t);  
26         ms.Position = 0;  
27         return (T)soap.Deserialize(ms);  
28     }  
29 }  
View Code

 

利用序列化和反序列化实现深拷贝

标签:epc   bsp   color   反序列化   bin   binary   orm   nbsp   des   

原文地址:https://www.cnblogs.com/Lite/p/9076517.html

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