▩▩ 프로그래밍 ▩▩/C#
[C#] byte[] 를 구조체로 바꾸기
남쿤
2013. 4. 18. 10:30
반응형
private static T ReadStruct<T>(byte[] buffer) where T : struct
{
int size = Marshal.SizeOf(typeof(T));
if (size > buffer.Length)
throw new Exception();
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.Copy(buffer, 0, ptr, size);
T obj = (T)Marshal.PtrToStructure(ptr, typeof(T));
Marshal.FreeHGlobal(ptr);
return obj;
}
struct A
{
public int x, y;
}
A a = ReadStruct<A>(buffer);
출처 : http://zsoo.net/35
반응형