Created
July 25, 2015 05:22
-
-
Save yuka1984/bc04af708ac3ff9d4120 to your computer and use it in GitHub Desktop.
C#でstructをバイト配列に変換する拡張メソッド ref: http://qiita.com/yu_ka1984/items/969728290b05e15f07a9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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