C#復(fù)制和深度復(fù)制的實(shí)現(xiàn)方法
深度復(fù)制與淺表復(fù)制的區(qū)別在于,淺表復(fù)制只復(fù)制值類型的值,而對于實(shí)例所包含的對象依然指向原有實(shí)例。
class Program
{
[Serializable]
public class Car
{
public string name;
public Car(string name)
{
this.name = name;
}
}
[Serializable]
public class Person:ICloneable
{
public int id;
public string name;
public Car car;
public Person()
{
}
public Person(int id, string name, Car car)
{
this.id = id;
this.name = name;
this.car = car;
}
public Object Clone() //實(shí)現(xiàn)ICloneable接口,達(dá)到淺表復(fù)制。淺表復(fù)制與深度復(fù)制無直接有關(guān)系。 對外提供一個創(chuàng)建自身的淺表副本的能力
{
return this.MemberwiseClone();
}
}
//要復(fù)制的實(shí)例必須可序列化,包括實(shí)例引用的其它實(shí)例都必須在類定義時加[Serializable]特性。
public static T Copy<T>(T RealObject)
{
using (Stream objectStream = new MemoryStream())
{
//利用 System.Runtime.Serialization序列化與反序列化完成引用對象的復(fù)制
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(objectStream, RealObject);
objectStream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(objectStream);
}
}
static void Main(string[] args)
{
Person p1 = new Person(1, "Scott", new Car("寶馬"));
Console.WriteLine("原始值:P1:id={0}----------->name={1}------>car={2}", p1.id, p1.name, p1.car.name);
Person p2 = Copy<Person>(p1); //克隆一個對象
Person p3 = p1.Clone() as Person;//淺表復(fù)制
Console.WriteLine("改變P1的值");
p1.id = 2;
p1.name = "Lacy";
p1.car.name = "紅旗";
Console.WriteLine("P1:id={0}----------->name={1}------>car={2}", p1.id, p1.name, p1.car.name);
Console.WriteLine("深度復(fù)制:P2:id={0}----------->name={1}------>car={2}", p2.id, p2.name, p2.car.name);
Console.WriteLine("淺表復(fù)制:P3:id={0}----------->name={1}------>car={2}", p3.id, p3.name, p3.car.name);
Console.ReadKey();
}
運(yùn)行結(jié)果:
一、List<T>對象中的T是值類型的情況(int 類型等)
對于值類型的List直接用以下方法就可以復(fù)制:
List<T> oldList = new List<T>(); oldList.Add(..); List<T> newList = new List<T>(oldList);
二、List<T>對象中的T是引用類型的情況(例如自定義的實(shí)體類)
1、對于引用類型的List無法用以上方法進(jìn)行復(fù)制,只會復(fù)制List中對象的引用,可以用以下擴(kuò)展方法復(fù)制:
static class Extensions
{
public static IList<T> Clone<T>(this IList<T> listToClone) where T: ICloneable
{
return listToClone.Select(item => (T)item.Clone()).ToList();
}
//<SPAN style="COLOR: #000000">當(dāng)然前題是List中的對象要實(shí)現(xiàn)ICloneable接口</SPAN>
}
2、另一種用序列化的方式對引用對象完成深拷貝,此種方法最可靠
public static T Clone<T>(T RealObject)
{
using (Stream objectStream = new MemoryStream())
{
//利用 System.Runtime.Serialization序列化與反序列化完成引用對象的復(fù)制
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(objectStream, RealObject);
objectStream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(objectStream);
}
}
3、利用System.Xml.Serialization來實(shí)現(xiàn)序列化與反序列化
public static T Clone<T>(T RealObject)
{
using(Stream stream=new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(stream, RealObject);
stream.Seek(0, SeekOrigin.Begin);
return (T)serializer.Deserialize(stream);
}
}
以上這篇C#復(fù)制和深度復(fù)制的實(shí)現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持我們。
上一篇:C語言數(shù)據(jù)結(jié)構(gòu) 鏈表與歸并排序?qū)嵗斀?/a>
欄 目:C語言
下一篇:c#中實(shí)現(xiàn)退出程序后自動重新啟動程序的方法
本文標(biāo)題:C#復(fù)制和深度復(fù)制的實(shí)現(xiàn)方法
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1800.html
您可能感興趣的文章


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)


