I am trying to create a byte array from the object. I have clone it but still encounter this exception. What is the problem here?
class ByteInteract
{
Object tempObj;
public ByteInteract(DataObj obj)
{
tempObj = (Object)obj.Clone();
}
public byte[] ToByteArray()
{
if (tempObj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
lock (tempObj)
{
ms.Flush();
bf.Serialize(ms, tempObj);
}
return ms.ToArray();
}
}
}