using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace Common { public sealed class StructConverter { public static int SizeOf(object structObj) { unsafe { return Marshal.SizeOf(structObj); } } public static int SizeOf(Type type) { return Marshal.SizeOf(type); } public static int SizeOf() { return Marshal.SizeOf(typeof(T)); } /// /// 结构体转成字节数组 /// /// /// public static byte[] StructToBytes(object structObj) { int size = SizeOf(structObj); byte[] bytes = new byte[size]; IntPtr structPtr = Marshal.AllocHGlobal(size); try { Marshal.StructureToPtr(structObj, structPtr, false); Marshal.Copy(structPtr, bytes, 0, size); } finally { Marshal.FreeHGlobal(structPtr); } return bytes; } /// /// 字节数组转成结构体 /// /// /// /// public static object BytesToStruct(byte[] bytes, Type type) { return BytesToStruct(bytes, 0, type); } /// /// 字节数组转成结构体 /// /// /// /// /// public static object BytesToStruct(byte[] bytes, int startIndex, Type type) { object structObj = null; int size = Marshal.SizeOf(type); if (startIndex < 0 || startIndex >= bytes.Length || size > bytes.Length) { return null; } IntPtr structPtr = Marshal.AllocHGlobal(size); try { Marshal.Copy(bytes, startIndex, structPtr, size); structObj = Marshal.PtrToStructure(structPtr, type); } finally { Marshal.FreeHGlobal(structPtr); } return structObj; } } }