Created
November 9, 2015 11:14
-
-
Save 0xF6/089e0adff6a80dbf8bf9 to your computer and use it in GitHub Desktop.
Read a structure in stream
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
using System; | |
using System.IO; | |
public unsafe static T Read<T>(this Stream s) where T : struct | |
{ | |
int i = System.Runtime.InteropServices.Marshal.SizeOf(typeof(T)); | |
byte[] buffer = new byte[i]; | |
s.Read(buffer, 0, i); | |
fixed (byte* pBuffer = &buffer[0]) | |
return (T)System.Runtime.InteropServices.Marshal.PtrToStructure((IntPtr)pBuffer, typeof(T)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment