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;
}
{
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;
}
ex)
struct A
{
public int x, y;
}
A a = ReadStruct<A>(buffer);
{
public int x, y;
}
A a = ReadStruct<A>(buffer);
'컴퓨터 프로그래밍' 카테고리의 다른 글
C# 의 static constructor (정적 생성자?) (0) | 2010.04.12 |
---|---|
Media Player Plugin 제작하기 - 2 바로 제작 (0) | 2009.12.18 |
Media Player Plugin 제작하기 - 1. 설치 (0) | 2009.12.17 |