Discussion on the deep copy problem of Listless thanTgreater than objects in C

  • 2020-05-12 02:53:16
  • OfStack

1. List < T > Object where T is a value type (int type, etc.)

For List of value type, it can be copied directly with the following method:


List<T> oldList = new List<T>(); 
oldList.Add(..); 
List<T> newList = new List<T>(oldList); 

2. List < T > T in an object is a case of a reference type (such as a custom entity class)

1. For List of reference type, the above method cannot be copied, only the reference of objects in List will be copied. The following extension method can be used to copy:


static class Extensions 
 { 
     public static IList<T> Clone<T>(this IList<T> listToClone) where T: ICloneable 
     { 
         return listToClone.Select(item => (T)item.Clone()).ToList(); 
     } 
 // And of course the previous one was List Object to be implemented ICloneable interface 
 } 

2, another way to serialize the reference object to complete the deep copy, this method is the most reliable


public static T Clone<T>(T RealObject) 

{ 
   using (Stream objectStream = new MemoryStream()) 
   { 
      // using  System.Runtime.Serialization Serialization and deserialization complete the replication of reference objects 
       IFormatter formatter = new BinaryFormatter(); 
       formatter.Serialize(objectStream, RealObject); 
       objectStream.Seek(0, SeekOrigin.Begin); 
       return (T)formatter.Deserialize(objectStream); 
   } 
}

3. Use System.Xml.Serialization to realize serialization and deserialization


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);
      }
}

3. Test the deep copy of the above objects

The tests are as follows:


using System;
using System.Collections.Generic;
using System.Collections ;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace LINQ
{
  [Serializable]
  public class tt
  {
    private string name = "";

    public string Name
    {
      get { return name; }
      set { name = value; }
    }
    private string sex = "";

    public string Sex
    {
      get { return sex; }
      set { sex = value; }
    }
  }

  class LINQTest
  {
    public static T Clone<T>(T RealObject) 
    { 
      using (Stream objectStream = new MemoryStream()) 
      { 
        IFormatter formatter = new BinaryFormatter(); 
        formatter.Serialize(objectStream, RealObject); 
        objectStream.Seek(0, SeekOrigin.Begin); 
        return (T)formatter.Deserialize(objectStream); 
      } 
    }


    public static void Main()
    {
      List<tt> lsttt = new List<tt>();
      tt tt1 = new tt();
      tt1.Name = "a1";
      tt1.Sex = "20";
      lsttt.Add(tt1);
      List<tt> l333 = new List<tt>();
      l333.Add(Clone<tt>(lsttt[0]));
      l333[0].Name = "333333333";
   }
 }
}

Related articles: