雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C#復(fù)制和深度復(fù)制的實(shí)現(xiàn)方法

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點(diǎ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

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有