1 |
#Region "Microsoft.VisualBasic::d84b39c59c0294a067990e3bc662355f, Microsoft.VisualBasic.Core\ComponentModel\Settings\Inf\ClassMapper.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
#End Region
|
44 |
|
45 |
Imports System.Runtime.CompilerServices
|
46 |
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
|
47 |
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel.SchemaMaps
|
48 |
Imports Microsoft.VisualBasic.Linq
|
49 |
|
50 |
Namespace ComponentModel.Settings.Inf
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
Public Module ClassMapper
|
56 |
|
57 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
58 |
Public Function MapParser(Of T As Class)() As NamedValue(Of BindProperty(Of DataFrameColumnAttribute)())
|
59 |
Return GetType(T).MapParser
|
60 |
End Function
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
<Extension>
|
68 |
Public Function MapParser(type As Type) As NamedValue(Of BindProperty(Of DataFrameColumnAttribute)())
|
69 |
Dim nameCLS As ClassName = type.GetAttribute(Of ClassName)
|
70 |
Dim name As String
|
71 |
|
72 |
If nameCLS Is Nothing Then
|
73 |
name = type.Name
|
74 |
Else
|
75 |
name = nameCLS.Name
|
76 |
End If
|
77 |
|
78 |
Dim source = DataFrameColumnAttribute.LoadMapping(type)
|
79 |
Dim binds = source.Values.ToArray
|
80 |
|
81 |
Return New NamedValue(Of BindProperty(Of DataFrameColumnAttribute)()) With {
|
82 |
.Name = name,
|
83 |
.Value = binds
|
84 |
}
|
85 |
End Function
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
<Extension>
|
95 |
Public Function ClassWriter(Of T As {New, Class})(ini As IniFile) As T
|
96 |
Return DirectCast(ClassWriter(ini, GetType(T)), T)
|
97 |
End Function
|
98 |
|
99 |
Public Function ClassWriter(ini As IniFile, type As Type) As Object
|
100 |
Dim obj As Object = Activator.CreateInstance(type)
|
101 |
Dim maps = MapParser(type)
|
102 |
|
103 |
For Each map In maps.Value
|
104 |
Dim key As String = map.field.Name
|
105 |
Dim value As String = ini.ReadValue(maps.Name, key)
|
106 |
Dim o As Object = Scripting.CTypeDynamic(value, map.Type)
|
107 |
Call map.SetValue(obj, o)
|
108 |
Next
|
109 |
|
110 |
Return obj
|
111 |
End Function
|
112 |
|
113 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
114 |
<Extension>
|
115 |
Public Sub ClassDumper(Of T As Class)(x As T, ini As IniFile)
|
116 |
Call ClassDumper(x, GetType(T), ini)
|
117 |
End Sub
|
118 |
|
119 |
Public Sub ClassDumper(x As Object, type As Type, ini As IniFile)
|
120 |
Dim maps = MapParser(type)
|
121 |
|
122 |
For Each map In maps.Value
|
123 |
Dim key As String = map.field.Name
|
124 |
Dim value As String = Scripting.ToString(map.GetValue(x))
|
125 |
Call ini.WriteValue(maps.Name, key, value)
|
126 |
Next
|
127 |
End Sub
|
128 |
|
129 |
|
130 |
Load a ini section profile data from a ini file.
|
131 |
|
132 |
<typeparam name="T">The section mapper</typeparam>
|
133 |
<param name="path">*.ini file</param>
|
134 |
|
135 |
|
136 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
137 |
<Extension>
|
138 |
Public Function LoadIni(Of T As {New, Class})(path As String) As T
|
139 |
Return New IniFile(path).ClassWriter(Of T)
|
140 |
End Function
|
141 |
|
142 |
|
143 |
Write ini section into data file.
|
144 |
|
145 |
|
146 |
<param name="x">A section class in the ini profile file.</param>
|
147 |
|
148 |
|
149 |
<Extension>
|
150 |
Public Function WriteClass(Of T As Class)(x As T, ini As String) As Boolean
|
151 |
Try
|
152 |
Call x.ClassDumper(New IniFile(ini))
|
153 |
Catch ex As Exception
|
154 |
ex = New Exception(ini, ex)
|
155 |
ex = New Exception(GetType(T).FullName, ex)
|
156 |
Call ex.PrintException
|
157 |
Call App.LogException(ex)
|
158 |
Return False
|
159 |
End Try
|
160 |
|
161 |
Return True
|
162 |
End Function
|
163 |
End Module
|
164 |
End Namespace
|