I have a struct, MyStruct
, that has a private member private bool[] boolArray;
and a method ChangeBoolValue(int index, bool Value)
. <
One simple method to make a (deep) copy, though not the fastest one (because it uses reflection), is to use BinaryFormatter
to serialize the original object to a MemoryStream
and then deserialize from that MemoryStream
to a new MyStruct
.
static public T DeepCopy(T obj)
{
BinaryFormatter s = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
s.Serialize(ms, obj);
ms.Position = 0;
T t = (T)s.Deserialize(ms);
return t;
}
}
Works for classes and structs.