转载:C# .NET Tips: Deep Cloning an Object

<script type="text/javascript"> </script> <script type="text/javascript" src="http://blog.joycode.com/WebResource.axd?d=o6m_bonedRmjjNzfF-Dv7Q2&t=633198064017968750"></script> <script type="text/javascript" src="http://blog.joycode.com/WebResource.axd?d=7idN8sjV8Q-YJNS2N2Jtx26uXOqZHr9KMGYoDl9Okn41&t=633198064017968750"></script> <script type="text/javascript"> </script>

JGTM'2007 [MVP]

Bridging the virtual world and the real world...
随笔 - 36 , 评论 - 447 , 引用 - 14, articles - 8

导航

公告

欢迎来信咨询、探讨任何您觉得我会感兴趣或者我能帮得上忙的话题!只是最近工作繁忙,若未能及时回复您的邮件,还望见谅!谢谢!:)

文章分类

存档

随笔分类

相册

Blogs I Read

C# .NET Tips: Deep Cloning an Object

2003年12月1日 22:01 - (阅读:5391;评论:9)

下面的工具函数可以构造source对象的deep copy,很有用!不妨一试 笑脸

using System.IO;
using System.Runtime.Serialization.Binary;

public object static DeepClone(object source)
{
? if (source==null) return null;

? using (MemoryStream stream = new MemoryStream())
? {
??? BinaryFormatter?formatter = new BinaryFormatter();
??? formatter.Serialize(stream, source);
??? stream.Position = 0;
??? return?formatter.Deserialize(stream);

? }

}

反馈

# 回复: C# .NET Tips: Deep Cloning an Object

2003-12-1 22:07 by mic
原来这样都可以,学到了:)

# 回复: C# .NET Tips: Deep Cloning an Object

2003-12-2 8:26 by kkwoof

object默认的clone是shallow copy :(

# 回复: C# .NET Tips: Deep Cloning an Object

2003-12-2 9:44 by JGTM'2003
@sam111:

Nice translation and expansion. In this tip I just shown an easy way to implement object cloning by means of "binary serialization". And because binary serialization is kind of deep serialization, the result is a deep cloned object graph. Thanks for your article for us all.

@kkwoof:

System.Object has a "protected" (but non-virtual) method called MemberwiseClone which is implemented to make a "shallow clone" of any object instance. Since the method is "protected", you need a way to "public"-ize the functionality by some means, say, IClonable or any public method. But if you wanna expose the fully recursively (I mean, deep) cloning functionality to the outside, this tip will be your first aid. :)

# 回复: C# .NET Tips: Deep Cloning an Object

2003-12-2 10:21 by kkwoof
sam111,请教一个问题:
BinaryFormatter和SoapFormatter以及.NET Remoting都使用Deep Serialization技术,。。。。。。。。。

你这里所说的deep,是不是说对哪些没有手工实现Iclonable的类?
如果自己实现了getobjectdata(),那么BinaryFormatter 还会
做深度的序列化吗?

# 回复: C# .NET Tips: Deep Cloning an Object

2003-12-2 13:51 by 开心就好
To JGTM:
上周你似乎询问过我,你应该写什么体裁的随笔,不过那时候没有什么概念,现在看了你在我的随笔以及别人随笔后的评论,我想我已经有思路了。
你可以虚拟一个团队,共同开发一个项目,这个项目最好通用一些,比如论坛的开发,然后就架构设计、实体类粒度等等来写一系列的随笔。以“讲故事”的方式给大家阐述一下你心目中完美的.NET的开发,从团队、技术、人员上等等来描述,不牵涉到人为因素。
在最后,你还可以再阐述一下在项目进度的压力下,如何简化开发模型,使开发即可以可控,又可以适应更快速开发的要求:D
不知道意下如何?
MSN上不去,没有办法跟你说了:D

# 回复: C# .NET Tips: Deep Cloning an Object

2003-12-2 16:35 by sam1111
hehe, your answer is very great:)

# re: C# .NET Tips: Deep Cloning an Object

2004-12-10 9:15 by wdf
public object static DeepClone(object source)
说source不支持Serialization,是怎么回事?

Post Comment

主题
姓名
主页  
校验码
内容 
<script type="text/javascript" src="http://www.google-analytics.com/urchin.js"> </script> <script type="text/javascript"> _uacct = "UA-247852-1"; urchinTracker(); </script>
<script type="text/javascript"> </script> <script type="text/javascript"> </script>
<script type="text/javascript"> </script>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值