0

I have to copy an Arraylist to a new Arraylist. In the old Arraylist there are multiple elements which changes at runtime.One of them is the module settings parameter inside the Arraylist. I would like to deep copy the values of the arraylist so that I can use it for undo operation. I tried using Binaryformater and Datacontract. Both can't be done for non serializable object. Can anyone help , pls?

2 Answers2

0

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

luxun
  • 457
  • 5
  • 14
0

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

KwackMaster
  • 180
  • 2
  • 13