Structure –>byte
1: Public Function RawSerialize(o As Object) As Byte()
2: Dim rawSize As Integer = Marshal.SizeOf(o)
3: Dim buffer As IntPtr = Marshal.AllocHGlobal(rawSize)
4: Marshal.StructureToPtr(o, buffer, False)
5: Dim rawDatas As Byte() = New Byte(rawSize) {}
6: Marshal.Copy(buffer, rawDatas, 0, rawSize) 7: Marshal.FreeHGlobal(buffer) 8: Return rawDatas
9: End Function
byte—>Structure
1: Public Function RawDeserialize(rawdata As Byte(), position As Integer, t As Type) As Object
2: Dim rawsize As Integer = Marshal.SizeOf(t)
3: If rawsize > rawdata.Length Then
4: Return Nothing
5: End If
6: Dim buffer As IntPtr = Marshal.AllocHGlobal(rawsize)
7: Marshal.Copy(rawdata, position, buffer, rawsize) 8: Dim retobj As Object = Marshal.PtrToStructure(buffer, t)
9: Marshal.FreeHGlobal(buffer) 10: Return retobj
11: End Function