2019-08-11 12:22:38 +08:00
|
|
|
|
#Region "Microsoft.VisualBasic::4eca099d62522f9c3be910f7f77f99f3, Microsoft.VisualBasic.Core\My\Framework\IOHandler.vb"
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
2019-05-06 18:27:46 +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/>.
|
2019-05-01 09:37:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-06 18:27:46 +08:00
|
|
|
|
' /********************************************************************************/
|
2019-05-01 09:37:46 +08:00
|
|
|
|
|
2019-05-06 18:27:46 +08:00
|
|
|
|
' Summaries:
|
2019-05-01 09:37:46 +08:00
|
|
|
|
|
2019-05-06 18:27:46 +08:00
|
|
|
|
' Module IOHandler
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' Delegate Function
|
|
|
|
|
|
'
|
|
|
|
|
|
' Function: GetWrite, IsRegister, SaveJSON, SaveXml
|
|
|
|
|
|
'
|
|
|
|
|
|
' Sub: RegisterHandle
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' /********************************************************************************/
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
|
|
|
|
|
#End Region
|
|
|
|
|
|
|
|
|
|
|
|
Imports System.Text
|
2019-05-01 09:37:46 +08:00
|
|
|
|
Imports Microsoft.VisualBasic.Language.Default
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Imports Microsoft.VisualBasic.Serialization.JSON
|
|
|
|
|
|
|
2019-08-10 01:31:53 +08:00
|
|
|
|
Namespace My.FrameworkInternal
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Collection IO extensions
|
|
|
|
|
|
''' </summary>
|
2019-05-01 09:37:46 +08:00
|
|
|
|
''' <remarks>
|
|
|
|
|
|
''' 在这个模块之中删除了read函数,因为read是取决于输入的文件的具体格式的,并不是取决于代码的
|
|
|
|
|
|
''' 所以读取操作无意义
|
|
|
|
|
|
''' 但是对于write而言,则是可以通过代码来控制具体的输出文件的
|
|
|
|
|
|
''' </remarks>
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Public Module IOHandler
|
|
|
|
|
|
|
|
|
|
|
|
Public Delegate Function ISave(obj As IEnumerable, path As String, encoding As Encoding) As Boolean
|
|
|
|
|
|
|
2019-05-01 09:37:46 +08:00
|
|
|
|
ReadOnly saveWrite As New Dictionary(Of Type, ISave)
|
2019-05-22 19:02:42 +08:00
|
|
|
|
ReadOnly defaultWriter As New [Default](Of ISave)(AddressOf SaveJSON)
|
2019-05-01 09:37:46 +08:00
|
|
|
|
|
|
|
|
|
|
Public Function GetWrite(type As Type) As ISave
|
|
|
|
|
|
Return saveWrite.TryGetValue(type) Or defaultWriter
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
Public Function IsRegister(type As Type) As Boolean
|
|
|
|
|
|
Return saveWrite.ContainsKey(type)
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Function
|
|
|
|
|
|
|
2019-05-01 09:37:46 +08:00
|
|
|
|
Public Sub RegisterHandle(handle As ISave, type As Type)
|
|
|
|
|
|
saveWrite(type) = handle
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Public Function SaveJSON(obj As IEnumerable, path As String, encoding As Encoding) As Boolean
|
|
|
|
|
|
Return GetObjectJson(obj, obj.GetType).SaveTo(path, encoding)
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
Public Function SaveXml(obj As IEnumerable, path As String, encoding As Encoding) As Boolean
|
|
|
|
|
|
Return GetXml(obj, obj.GetType).SaveTo(path, encoding)
|
|
|
|
|
|
End Function
|
|
|
|
|
|
End Module
|
|
|
|
|
|
End Namespace
|