Knowledge Base Nr: 00289 deepcopy.cs - http://www.swe-kaiser.de
Downloads:
c#: make a deep copy of any serializable object
public Object Clone(Object Original)
{
System.IO.MemoryStream _Stream = null;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _Formatter = null;
Byte[] _Bytes = null;
Object _Clone = null;
try
{
_Stream = new MemoryStream();
_Formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
_Formatter.Serialize(_Stream, Original);
_Bytes = _Stream.ToArray();
_Stream.Close();
_Stream = new MemoryStream(_Bytes);
_Clone = _Formatter.Deserialize(_Stream);
_Stream.Close();
return _Clone;
}
finally
{
if (_Stream != null)
_Stream.Close();
_Stream = null;
}
}
//sample
private void dummyToolStripButton_Click(object sender, EventArgs e)
{
FRAMETYPE d1 = new FRAMETYPE();
d1.SIGNALINSTANCES = new SIGNALINSTANCETYPE[3];
d1.SIGNALINSTANCES[1] = new SIGNALINSTANCETYPE();
d1.SIGNALINSTANCES[1].BITPOSITION = "33";
FRAMETYPE d2 = (FRAMETYPE)Clone(d1);
}