static public void structToByteArray(PageArrayBin bin, ref byte[] result) { int size = Marshal.SizeOf(bin); IntPtr buffer = Marshal.AllocHGlobal(size); result = new byte[size]; Marshal.StructureToPtr(bin, buffer, true); Marshal.Copy(buffer, result, 0, size); Marshal.FreeHGlobal(buffer); }
static public void byteArrayToStruct(byte[] result, ref PageArrayBin bin) { //实际大小 int size = result.Length; //结构体大小 int totalSize = Marshal.SizeOf(bin); IntPtr buffer = Marshal.AllocHGlobal(totalSize); Marshal.Copy(result, 0, buffer, size); bin = (PageArrayBin)Marshal.PtrToStructure(buffer, typeof(PageArrayBin)); Marshal.FreeHGlobal(buffer); }