1 | #Region "Microsoft.VisualBasic::9ff611a653fa220ff3cb1c4561e6d225, Microsoft.VisualBasic.Core\Serialization\BinaryDumping\StructSerializer.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Module StructSerializer |
35 | ' |
36 | ' Function: ByteToStructure, StructureToByte |
37 | ' |
38 | ' |
39 | ' /********************************************************************************/ |
40 | |
41 | #End Region |
42 | |
43 | Imports System.Runtime.CompilerServices |
44 | Imports System.Runtime.InteropServices |
45 | |
46 | Namespace Serialization.BinaryDumping |
47 | |
48 | ''' <summary> |
49 | ''' Some times these method is not works well, not sure why? |
50 | ''' </summary> |
51 | ''' <remarks> |
52 | ''' http://blog.csdn.net/zztoll/article/details/8695992 |
53 | ''' |
54 | ''' Marshal类的两个方法StructureToPtr和PtrToStructure实现序列化 |
55 | ''' |
56 | ''' 我们主要是使用Marshal类里的两个方法: |
57 | ''' |
58 | ''' 第一个是StructureToPtr,将数据从托管对象封送到非托管内存块。 |
59 | ''' 第二个是PtrToStructure,将数据从非托管内存块封送到新分配的指定类型的托管对象。 |
60 | ''' |
61 | ''' 只要有了这两个相互转换的方法,我们就可以实现序列化了。 |
62 | ''' |
63 | ''' 首先我们先来看下序列化 |
64 | ''' |
65 | ''' 序列化: |
66 | ''' |
67 | ''' 有一个前提条件,那就是我们必须要知道需要序列化对象的大小。 |
68 | ''' |
69 | ''' 第一步:我们先求出对象的大小,然后在非托管内存中给它分配相应的内存大小。 |
70 | ''' 第二步:接着我们就把这个对象封送到刚分配出来的内存中,之后我们会得到一个分配出来的内存块首地址指针。 |
71 | ''' 第三步:最后我们可以通过这个首地址指针,从指针所指的位置处开始,拷贝数据到指定的byte[]数组中, |
72 | ''' 拷贝的长度就是我们为这个对象分配出来的内存大小,得到byte[]数据后,下面的事情我就不用多说了, |
73 | ''' 你可以保存到数据库或者文件中。 |
74 | ''' |
75 | ''' 反序列化: |
76 | ''' |
77 | ''' 序列化的时候我们先将一个对象封送到了非托管内存块中,然后再把内存块中的数据读到byte[]数组中, |
78 | ''' |
79 | ''' 现在我们反序列化 |
80 | ''' |
81 | ''' 第一步:我们先求出对象的大小,然后在非托管内存中给它分配相应的内存大小。 |
82 | ''' 第二步:然后把这个byte[]数据拷贝到非托管内存块中。 |
83 | ''' 第三步:最后再从内存块中封送指定大小的数据到对象中。 |
84 | ''' |
85 | ''' 有一个地方需要注意,那就是因为引用类型的对象我们是无法求的它的实际大小的,所以这里的对象我们只能使用非托管对象,比如struct结构体。 |
86 | ''' 所以,当我们只是用来存储数据,不涉及任何操作的对象,我们可以把它作为一个结构体来处理,这样我们在序列化的时候可以节省空间开销。 |
87 | ''' 因为你如果你要是用平常的序列化方法去序列化一个类对象,他所需要的空间开销是要大于你去序列化一个具有相同结构的struct对象。 |
88 | ''' </remarks> |
89 | Public Module StructSerializer |
90 | |
91 | ' 2016.5.20 debugger exception: |
92 | ' ' Unhandled Exception: System.AccessViolationException: Attempted to read Or write protected memory. This Is often an indication that other memory Is corrupt. |
93 | ' at System.Runtime.InteropServices.Marshal.StructureToPtr(Object Structure, IntPtr ptr, Boolean fDeleteOld) |
94 | ' at Microsoft.VisualBasic.Serialization.BinaryDumping.StructSerializer.StructureToByte[T](T struct) |
95 | ' at EasyDocument.Program.Main() |
96 | |
97 | ' These two function will not works, prefer to the extensions in StructFormatter Module |
98 | |
99 | ''' <summary> |
100 | ''' 由结构体转换为byte数组(字符串类型以及Class类型都将会被序列化为内存指针,所以这个函数只适合于值类型的) |
101 | ''' </summary> |
102 | ''' |
103 | <Extension> |
104 | Public Function StructureToByte(Of T As Structure)(struct As T) As Byte() |
105 | Dim size As Integer = Marshal.SizeOf(GetType(T)) |
106 | Dim buffer As Byte() = New Byte(size - 1) {} |
107 | Dim bufferIntPtr As IntPtr = Marshal.AllocHGlobal(size) |
108 | Try |
109 | Marshal.StructureToPtr(struct, bufferIntPtr, True) |
110 | Marshal.Copy(bufferIntPtr, buffer, 0, size) |
111 | Finally |
112 | Marshal.FreeHGlobal(bufferIntPtr) |
113 | End Try |
114 | Return buffer |
115 | End Function |
116 | |
117 | ''' <summary> |
118 | ''' 由byte数组转换为结构体(字符串类型以及Class类型都将会被序列化为内存指针,所以这个函数只适合于值类型的) |
119 | ''' </summary> |
120 | ''' |
121 | <Extension> |
122 | Public Function ByteToStructure(Of T As Structure)(dataBuffer As Byte()) As T |
123 | Dim [structure] As Object = Nothing |
124 | Dim size As Integer = Marshal.SizeOf(GetType(T)) |
125 | Dim allocIntPtr As IntPtr = Marshal.AllocHGlobal(size) |
126 | Try |
127 | Marshal.Copy(dataBuffer, 0, allocIntPtr, size) |
128 | [structure] = Marshal.PtrToStructure(allocIntPtr, GetType(T)) |
129 | Finally |
130 | Marshal.FreeHGlobal(allocIntPtr) |
131 | End Try |
132 | Return DirectCast([structure], T) |
133 | End Function |
134 | End Module |
135 | End Namespace |