我如何在没有icloneable接口的情况下深度克隆windows phone 8.1中的列表<T>?

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

我想深入克隆一个通用列表,但是 windows phone 8.1 中不存在 icloneable 接口 class 也不存在?

您可以创建自己的icloneable接口并定义一个函数如下:

 public interface IClonable<T>
 {
     T Clone();
 }

 public static T[] Clone<T>(this T[] origin) where T : IClonable<T>
 {
     return origin.Select(x => x.Clone()).ToArray();
 }

试试这个

 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:

如果你想克隆所有成员然后引用这个Deep Copy of Object in C#