---
title: "DeepClone与ShadowClone(downmoon)"
author: "netxdiy"
date: "2007-12-24T23:19:00Z"
tags: []
slug: "deepclone-shadowclone-downmoon"
draft: false
lastmod: "2012-09-27T11:31:30Z"
id: 1687107
url: "https://netxdiy.iteye.com/blog/1687107"
---

杳MSDN知：  
ArrayList.Clone 方法

创建ArrayList 的浅表副本。

命名空间:System.Collections  
程序集:mscorlib（在 mscorlib.dll 中）

返回值ArrayList 的浅表副本。

集合的浅表副本仅复制集合的元素（不论它们是引用类型还是值类型），但不复制引用所引用的对象。新集合中的引用与原始集合中的引用指向相同的对象。

与之相对，集合的深层副本将复制这些元素以及由它们直接或间接引用的所有内容。

据此，说明如下　：

![](http://botu.bokee.com/photodata1/2007-12-24/005/628/748/12923372/12923372.jpg)

![](http://botu.bokee.com/photodata1/2007-12-24/005/628/748/12923371/12923371.jpg)

下面给出一个完整的例子：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using System;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) using System.Collections.Generic;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) using System.Text;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) using System.IO;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) using System.Runtime.Serialization.Formatters.Binary;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) namespace CloneObjectTest  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // Justaddedtoshowthedifferencebetweenshallowanddeepcloning  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) \[Serializable()]  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) class SampleRefType  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public int Val;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) \[Serializable()]  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) class Employee  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public string m\_Name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public Int16m\_age;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public SampleRefTypesomeObject;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public void Print()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Console.WriteLine( " Name: " + m\_Name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Console.WriteLine( " Age: " + m\_age);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Console.WriteLine( " SomeValue: " + someObject.Val);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // Thisisonewaytododeepcloning.Butworksonlyifthe  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // objectsanditsreferencesareserializable  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public EmployeeDeepClone()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) MemoryStreamm = new MemoryStream();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) BinaryFormatterb = new BinaryFormatter();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) b.Serialize(m, this );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) m.Position = 0 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) return (Employee)b.Deserialize(m);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // Doshallowcloning  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public EmployeeShallowClone()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) return (Employee) this .MemberwiseClone();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) static void Main( string \[]args)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) SampleRefTypeobjRef = new SampleRefType();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) objRef.Val = 1000 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) EmployeeobjA = new Employee();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) objA.m\_Name = " Manoj " ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) objA.m\_age = 23 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) objA.someObject = objRef;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) EmployeeobjB = objA.DeepClone();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) EmployeeobjC = objA.ShallowClone();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // objBisadeepclone.SochagesmadetoobjRefwouldnotaffectobjB'svalue.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // ButobjCisshallowcloned.So,iftheobjRefischanged,soisthevaluein  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // ObjC.SoobjCshouldprint2000whereasobjBshouldprint1000  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) objRef.Val = 2000 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // Updatingsomestate  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) objC.m\_age = 100 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) objB.m\_Name = " NewName " ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Console.WriteLine( " OriginalObject: " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) objA.Print();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Console.WriteLine();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Console.WriteLine( " ShallowClonedObject: " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) objC.Print();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Console.WriteLine();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Console.WriteLine( " DeepClonedObject: " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) objB.Print();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)