microsoft-visualbasic-runtime/Serialization/BinaryDumping/StructFormatter.vb

101 lines
3.7 KiB
VB.net
Raw Normal View History

2019-06-15 17:34:52 +08:00
#Region "Microsoft.VisualBasic::643441f00248ad1e5d3cf531e26528f1, Microsoft.VisualBasic.Core\Serialization\BinaryDumping\StructFormatter.vb"
2019-03-22 22:19:09 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
' Module StructFormatter
'
' Function: DeSerialize, GetSerializeBuffer, Load, Serialize
'
'
' /********************************************************************************/
2018-08-02 20:14:48 +08:00
#End Region
Imports System.IO
Imports System.Runtime.CompilerServices
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Namespace Serialization.BinaryDumping
Public Module StructFormatter
''' <summary>
''' Save a structure type object into a binary file.(使用二进制序列化保存一个对象)
''' </summary>
''' <typeparam name="T"></typeparam>
''' <param name="obj"></param>
''' <param name="path"></param>
''' <returns></returns>
''' <remarks></remarks>
2019-03-21 19:25:10 +08:00
'''
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2018-08-02 20:14:48 +08:00
<Extension> Public Function Serialize(Of T)(obj As T, path As String) As Boolean
2019-03-21 19:25:10 +08:00
Return obj.GetSerializeBuffer.FlushStream(path)
2018-08-02 20:14:48 +08:00
End Function
<Extension> Public Function GetSerializeBuffer(Of T)(obj As T) As Byte()
Dim IFormatter As IFormatter = New BinaryFormatter()
2019-03-21 19:25:10 +08:00
Using stream As New MemoryStream()
Call IFormatter.Serialize(stream, obj)
Dim buffer As Byte() = stream.ToArray
Return buffer
End Using
2018-08-02 20:14:48 +08:00
End Function
<Extension> Public Function DeSerialize(Of T)(bytes As Byte()) As T
Dim obj As Object = (New BinaryFormatter).[Deserialize](New MemoryStream(bytes))
Return DirectCast(obj, T)
End Function
''' <summary>
''' Load a strucutre object from the file system by using binary serializer deserialize.
''' (使用反二进制序列化从指定的文件之中加载一个对象)
''' </summary>
''' <typeparam name="T"></typeparam>
''' <param name="path"></param>
''' <returns></returns>
''' <remarks></remarks>
<Extension> Public Function Load(Of T)(path As String) As T
2019-03-21 19:25:10 +08:00
If Not path.FileExists Then
2018-08-02 20:14:48 +08:00
Return Activator.CreateInstance(Of T)()
End If
2019-03-21 19:25:10 +08:00
Using stream As Stream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
2018-08-02 20:14:48 +08:00
Dim IFormatter As IFormatter = New BinaryFormatter()
2019-03-21 19:25:10 +08:00
Dim obj As T = DirectCast(IFormatter.Deserialize(stream), T)
2018-08-02 20:14:48 +08:00
Return obj
End Using
End Function
End Module
End Namespace