Search code examples
windows-phone-8windows-phone-8.1

how can i deep clone a list<T> in windows phone 8.1 without icloneable interface?


I want to deep clone a generic list but icloneable interface is not present in windows phone 8.1 also binaryformatter class is also not present?


Solution

  • Try this

     using System.IO;
     using System.Runtime.Serialization.Formatters.Binary;
    
    public static T DeepClone<T>(T obj)
    {
      using (var ms = new MemoryStream())
      {
          var formatter = new BinaryFormatter();
          formatter.Serialize(ms, obj);
          ms.Position = 0;
          return (T) formatter.Deserialize(ms);
      }
    }
    
    • Your class MUST be marked as [Serializable] in order for this to work.
    • Your source file must include the following code:

    If you want clone all members and then refer this Deep Copy of Object in C#