Is there a way to deep copy non serializable object?

This question already has an answer here:

  • Deep cloning objects 39 answers

  • Without more information it's hard to say exactly what's wrong, but ArrayList itself is serializable. However, if you are using a custom object you need to mark it as serializable for serialization to work. See: Serialize ArrayList of Objects


    Use a memory stream and binary formatter Something like

    `public T Clone<T> (T obj)
    .   {
    .       using(var ms = new MemoryStream())
    .       {
    .          var formatter = new BinaryFormatter();
            formatter.Serialize(stream, obj);
            stream.Position =0;
            return (T)formatter.Deserialize(stream);
         }
    .    }’
    

    Hope that helps

    链接地址: http://www.djcxy.com/p/40780.html

    上一篇: 简单的方法来实现深层克隆C#

    下一篇: 有没有办法深入复制不可序列化的对象?