字节数组转换成结构

据说是可以将字节数组转换成结构。我还没有测试。等要用的时候再来看看吧。先记到这儿。
using System.Runtime.InteropServices;

static object BytesToStruct(byte[] bytes, Type strcutType)
{
int size = Marshal.SizeOf(strcutType);
IntPtr buffer 
= Marshal.AllocHGlobal(size);
try
{
Marshal.Copy(bytes, 
0, buffer, size);
return Marshal.PtrToStructure(buffer, strcutType);
}

finally
{
Marshal.FreeHGlobal(buffer);
}

}


posted @ 2005-02-07 13:01  浮云  阅读(977)  评论(1编辑  收藏  举报