public static byte[] ConvertStructToBytes(object obj)
{
int structSize = Marshal.SizeOf(obj);
byte[] allData = new byte[structSize];
GCHandle handle = GCHandle.Alloc(allData, GCHandleType.Pinned);
Marshal.StructureToPtr(obj, handle.AddrOfPinnedObject(), false);
handle.Free();
return allData;
}