protected static byte Serialize object xobject byte result new byte Ma

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
protected static byte[] Serialize(object xobject)
{
byte[] result = new byte[Marshal.SizeOf(xobject)];
GCHandle handle = GCHandle.Alloc(result, GCHandleType.Pinned);
IntPtr pointer = handle.AddrOfPinnedObject();
Marshal.StructureToPtr(xobject, pointer, false);
handle.Free();
return result;
}
protected static object Deserialize(byte[] raw, Type xtype)
{
GCHandle handle = GCHandle.Alloc(raw, GCHandleType.Pinned);
IntPtr pointer = handle.AddrOfPinnedObject();
object result = Marshal.PtrToStructure(pointer, xtype);
handle.Free();
return result;
}