public static byte[] ToByteArray<T>(this T structure) where T : struct
{
byte[] bb = new byte[Marshal.SizeOf(typeof(T))];
GCHandle gch = GCHandle.Alloc(bb, GCHandleType.Pinned);
Marshal.StructureToPtr(structure, gch.AddrOfPinnedObject(), false);
gch.Free();
return bb;
}
よく使うネタです。