Skip to content

Instantly share code, notes, and snippets.

@yuka1984
Created July 25, 2015 05:22
Show Gist options
  • Save yuka1984/bc04af708ac3ff9d4120 to your computer and use it in GitHub Desktop.
Save yuka1984/bc04af708ac3ff9d4120 to your computer and use it in GitHub Desktop.
C#でstructをバイト配列に変換する拡張メソッド ref: http://qiita.com/yu_ka1984/items/969728290b05e15f07a9
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment