C#byte数组转结构体

 public object BytesToStruct(byte[] bytes, Type strcutType)
        {
            int Size;
            IntPtr ptr;
            object obj;
            Size = Marshal.SizeOf(strcutType);
            ptr = Marshal.AllocHGlobal(Size);
            try
            {
                Marshal.Copy(bytes, 0, ptr, Size);
                obj = Marshal.PtrToStructure(ptr, strcutType);
                return obj;
            }
            finally
            {
                Marshal.FreeHGlobal(ptr);
            }           
        }

你可能感兴趣的:(C#byte数组转结构体)