1 #Region "Microsoft.VisualBasic::4877d517fb4e974cb62dd74588786cac, Microsoft.VisualBasic.Core\Serialization\ConfigMappings\DynamicsConfiguration.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     '     Class DynamicsConfiguration
35     
36     '         FunctionGetDynamicMemberNames, GetProperties, LoadDocument, ToDictionary, TryConvert
37     '                   TryGetMember, TrySetMember
38     
39     
40     ' /********************************************************************************/
41
42 #End Region
43
44 Imports System.Reflection
45 Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
46 Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel.DataFramework
47 Imports Microsoft.VisualBasic.Serialization.JSON
48
49 Namespace Serialization
50
51     Public Class DynamicsConfiguration : Inherits Dynamic.DynamicObject
52
53         ''' <summary>
54         ''' 加载完数据之后返回其自身
55         ''' </summary>
56         ''' <typeparam name="T"></typeparam>
57         ''' <param name="path"></param>
58         ''' <returns></returns>
59         ''' <remarks></remarks>
60         Public Function LoadDocument(Of T)(path As StringAs Object
61             Dim TextChunk As String() = IO.File.ReadAllLines(path)
62
63             Return Me
64         End Function
65
66         Public Shared Function GetProperties(Of T)(access As PropertyAccess) As Dictionary(Of StringPropertyInfo)
67             Return Schema(Of T)(access)
68         End Function
69
70         ''' <summary>
71         ''' 将目标dump为一个字典
72         ''' </summary>
73         ''' <typeparam name="T"></typeparam>
74         ''' <param name="onlyPrimitive">
75         ''' 假若这个为真的话,则只有初级类型的才会被读取,反之,复杂类型会被序列化为json作为字符串value
76         ''' </param>
77         ''' <returns></returns>
78         Public Shared Function ToDictionary(Of T)(Optional onlyPrimitive As Boolean = TrueAs Func(Of T, Dictionary(Of StringString))
79             Dim ps = Schema(Of T)(PropertyAccess.Readable)
80
81             For Each p As String In ps.Keys.ToArray
82                 If Not ps(p).GetIndexParameters.IsNullOrEmpty Then
83                     Call ps.Remove(p)
84                 End If
85             Next
86
87             Return Function(o)
88                        Dim out As New Dictionary(Of StringString)
89
90                        For Each p As PropertyInfo In ps.Values
91                            Dim pType As Type = p.PropertyType
92                            Dim value As Object = p.GetValue(o, Nothing)
93
94                            If IsPrimitive(pType) Then
95                                out(p.Name) = Scripting.ToString(value)
96                            Else
97                                If Not onlyPrimitive Then
98                                    out(p.Name) = pType.GetObjectJson(value)
99                                End If
100                            End If
101                        Next
102
103                        Return out
104                    End Function
105         End Function
106
107 #Region "Dynamics Support"
108
109         Public Overrides Function GetDynamicMemberNames() As IEnumerable(Of String)
110             Return MyBase.GetDynamicMemberNames()
111         End Function
112
113         Public Overrides Function TryGetMember(binder As Dynamic.GetMemberBinder, ByRef result As ObjectAs Boolean
114             Return MyBase.TryGetMember(binder, result)
115         End Function
116
117         Public Overrides Function TrySetMember(binder As Dynamic.SetMemberBinder, value As ObjectAs Boolean
118             Return MyBase.TrySetMember(binder, value)
119         End Function
120
121         Public Overrides Function TryConvert(binder As Dynamic.ConvertBinder, ByRef result As ObjectAs Boolean
122             Return MyBase.TryConvert(binder, result)
123         End Function
124 #End Region
125     End Class
126 End Namespace