1 |
#Region "Microsoft.VisualBasic::7b06ea52dd2da50d815859fef8b03eae, Microsoft.VisualBasic.Core\ComponentModel\Settings\ConfigEngine.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 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
#End Region
|
50 |
|
51 |
Imports System.Reflection
|
52 |
Imports System.Runtime.CompilerServices
|
53 |
Imports System.Text
|
54 |
Imports Microsoft.VisualBasic.CommandLine.Reflection
|
55 |
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
|
56 |
Imports Microsoft.VisualBasic.Language
|
57 |
Imports Microsoft.VisualBasic.Linq
|
58 |
|
59 |
Namespace ComponentModel.Settings
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
Public Class ConfigEngine : Inherits ITextFile
|
66 |
Implements IDisposable
|
67 |
|
68 |
|
69 |
所映射的数据源
|
70 |
|
71 |
Protected settingsData As IProfile
|
72 |
|
73 |
键名都是小写的
|
74 |
|
75 |
Protected profileItemCollection As IDictionary(Of String, BindMapping)
|
76 |
|
77 |
|
78 |
List all of the available settings nodes in this profile data session.
|
79 |
(枚举出当前配置会话之中的所有可用的配置节点)
|
80 |
|
81 |
<value></value>
|
82 |
<returns></returns>
|
83 |
<remarks></remarks>
|
84 |
Public ReadOnly Property AllItems As BindMapping()
|
85 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
86 |
Get
|
87 |
Return profileItemCollection _
|
88 |
.Values _
|
89 |
.ToArray
|
90 |
End Get
|
91 |
End Property
|
92 |
|
93 |
Public Overrides Property FilePath As String
|
94 |
Get
|
95 |
Return settingsData.FilePath
|
96 |
End Get
|
97 |
Set(value As String)
|
98 |
settingsData.FilePath = value
|
99 |
End Set
|
100 |
End Property
|
101 |
|
102 |
Sub New(obj As IProfile)
|
103 |
settingsData = obj
|
104 |
profileItemCollection = ConfigEngine.Load(Of IProfile)(
|
105 |
obj.GetType,
|
106 |
obj:=settingsData
|
107 |
).ToDictionary(Function(x) x.Name,
|
108 |
Function(x) x.Value)
|
109 |
End Sub
|
110 |
|
111 |
Protected Sub New()
|
112 |
End Sub
|
113 |
|
114 |
Protected Friend Shared Function Load(Of EntityType)(type As Type, obj As EntityType) As NamedValue(Of BindMapping)()
|
115 |
Dim LQuery As IEnumerable(Of BindMapping) =
|
116 |
_
|
117 |
From [Property] As PropertyInfo
|
118 |
In type.GetProperties
|
119 |
Let attributes = [Property].GetCustomAttributes(attributeType:=ProfileItemType, inherit:=False)
|
120 |
Where attributes.Length > 0
|
121 |
Let attr = DirectCast(attributes(0), ProfileItem)
|
122 |
Select BindMapping.Initialize(attr, [Property], obj) '
|
123 |
|
124 |
Dim out As List(Of NamedValue(Of BindMapping)) =
|
125 |
LinqAPI.MakeList(Of NamedValue(Of BindMapping)) <=
|
126 |
_
|
127 |
From ProfileItem As BindMapping
|
128 |
In LQuery
|
129 |
Let name As String = GetName(ProfileItem, ProfileItem.BindProperty)
|
130 |
Select New NamedValue(Of BindMapping) With {
|
131 |
.Name = name,
|
132 |
.Value = ProfileItem
|
133 |
}
|
134 |
|
135 |
Dim Nodes = From [property] As PropertyInfo
|
136 |
In type.GetProperties
|
137 |
Let attributes = [property].GetCustomAttributes(attributeType:=ProfileItemNode, inherit:=False)
|
138 |
Where attributes.Length = 1
|
139 |
Select New With {
|
140 |
.[Property] = [property],
|
141 |
.Entity = [property].GetValue(obj, Nothing)
|
142 |
}
|
143 |
|
144 |
Dim innerNodes = Nodes.ToArray
|
145 |
|
146 |
If innerNodes.Length > 0 Then
|
147 |
For Each x In innerNodes
|
148 |
|
149 |
If x.Entity Is Nothing Then
|
150 |
Try
|
151 |
x.Entity = Activator.CreateInstance(type:=x.[Property].PropertyType)
|
152 |
Catch ex As Exception
|
153 |
Dim view As String =
|
154 |
$"{x.Property.Name} As {x.Property.PropertyType.FullName}"
|
155 |
ex = New Exception(view, ex)
|
156 |
|
157 |
Throw ex
|
158 |
Finally
|
159 |
Call x.Property.SetValue(obj, x.Entity, Nothing)
|
160 |
End Try
|
161 |
End If
|
162 |
|
163 |
out += Load(x.[Property].PropertyType, x.Entity)
|
164 |
Next
|
165 |
|
166 |
Return out.ToArray
|
167 |
End If
|
168 |
|
169 |
Return out.ToArray
|
170 |
End Function
|
171 |
|
172 |
Protected Shared Function GetName(ProfileItem As ProfileItem, [Property] As PropertyInfo) As String
|
173 |
If String.IsNullOrEmpty(ProfileItem.Name) Then
|
174 |
ProfileItem.Name = [Property].Name.ToLower
|
175 |
End If
|
176 |
Return ProfileItem.Name.ToLower
|
177 |
End Function
|
178 |
|
179 |
|
180 |
大小写不敏感
|
181 |
|
182 |
<param name="Name"></param>
|
183 |
<returns></returns>
|
184 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
185 |
<ExportAPI("Node.Exists")>
|
186 |
Public Overridable Function ExistsNode(Name As String) As Boolean
|
187 |
Return profileItemCollection.ContainsKey(Name.ToLower)
|
188 |
End Function
|
189 |
|
190 |
|
191 |
请注意,<paramref name="name"/>必须是小写的
|
192 |
|
193 |
<param name="Name">The name of the configuration entry should be in lower case.</param>
|
194 |
<param name="Value"></param>
|
195 |
<returns></returns>
|
196 |
|
197 |
<ExportAPI("SetValue")>
|
198 |
Public Overridable Function [Set](Name As String, Value As String) As Boolean
|
199 |
Dim keyFind As String = Name.ToLower
|
200 |
|
201 |
If profileItemCollection.ContainsKey(keyFind) Then
|
202 |
Call profileItemCollection(keyFind).Set(Value)
|
203 |
Else
|
204 |
Return False
|
205 |
End If
|
206 |
|
207 |
Return True
|
208 |
End Function
|
209 |
|
210 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
211 |
Public Function [Set](var As NamedValue(Of String)) As Boolean
|
212 |
Return [Set](var.Name, var.Value)
|
213 |
End Function
|
214 |
|
215 |
<ExportAPI("GetValue")>
|
216 |
Public Overridable Function GetSettings(Name As String) As String
|
217 |
Dim keyFind As String = Name.ToLower
|
218 |
|
219 |
If profileItemCollection.ContainsKey(keyFind) Then
|
220 |
Dim item = profileItemCollection(keyFind)
|
221 |
Dim result = item.Value
|
222 |
Return result
|
223 |
Else
|
224 |
Return ""
|
225 |
End If
|
226 |
End Function
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
<param name="name">假若本参数为空,则函数输出所有的变量的值,大小写不敏感的</param>
|
232 |
<returns></returns>
|
233 |
<remarks></remarks>
|
234 |
|
235 |
<ExportAPI("View")>
|
236 |
Public Overridable Function View(Optional name As String = "") As String
|
237 |
If String.IsNullOrEmpty(name) Then
|
238 |
Return Prints(Me.profileItemCollection.Values)
|
239 |
Else
|
240 |
Return GetSettingsNode(name).AsOutString
|
241 |
End If
|
242 |
End Function
|
243 |
|
244 |
<ExportAPI("Prints")>
|
245 |
Public Shared Function Prints(data As IEnumerable(Of BindMapping)) As String
|
246 |
Dim source As NamedValue(Of String)() =
|
247 |
LinqAPI.Exec(Of NamedValue(Of String)) <=
|
248 |
_
|
249 |
From x As BindMapping
|
250 |
In data
|
251 |
Let value As String =
|
252 |
If(String.IsNullOrEmpty(x.Value), "null", x.Value)
|
253 |
Select New NamedValue(Of String) With {
|
254 |
.Name = x.Name,
|
255 |
.Value = value,
|
256 |
.Description = x.Description
|
257 |
}
|
258 |
|
259 |
Return Prints(source)
|
260 |
End Function
|
261 |
|
262 |
<ExportAPI("Prints")>
|
263 |
Public Shared Function Prints(data As IEnumerable(Of NamedValue(Of String))) As String
|
264 |
Dim keys As String() = data.Select(Function(x) x.Name).ToArray
|
265 |
Dim maxLen As Integer = keys.Select(AddressOf Len).Max
|
266 |
Dim sb As New StringBuilder(New String("-"c, 120))
|
267 |
|
268 |
Call sb.AppendLine()
|
269 |
|
270 |
For Each line As NamedValue(Of String) In data
|
271 |
Dim blank As String = New String(" "c, maxLen - Len(line.Name) + 2)
|
272 |
Dim str As String = String.Format(" {0}{1} = {2}", line.Name, blank, line.Value)
|
273 |
|
274 |
If Not String.IsNullOrEmpty(line.Description) Then
|
275 |
str &= " // " & line.Description
|
276 |
End If
|
277 |
|
278 |
Call sb.AppendLine(str)
|
279 |
Next
|
280 |
|
281 |
Return sb.ToString
|
282 |
End Function
|
283 |
|
284 |
|
285 |
大小写不敏感的
|
286 |
|
287 |
<param name="Name"></param>
|
288 |
<returns></returns>
|
289 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
290 |
<ExportAPI("GetNode")>
|
291 |
Public Function GetSettingsNode(Name As String) As BindMapping
|
292 |
Return profileItemCollection(Name.ToLower)
|
293 |
End Function
|
294 |
|
295 |
Public Overrides Function ToString() As String
|
296 |
Return settingsData.FilePath
|
297 |
End Function
|
298 |
|
299 |
<ExportAPI("Save")>
|
300 |
Public Overrides Function Save(Optional FilePath As String = "", Optional Encoding As Encoding = Nothing) As Boolean
|
301 |
Dim Xml As String = settingsData.GetXml
|
302 |
Return Xml.SaveTo(getPath(FilePath), Encoding)
|
303 |
End Function
|
304 |
|
305 |
Protected Friend Shared ReadOnly Property ProfileItemType As Type = GetType(ProfileItem)
|
306 |
Protected Friend Shared ReadOnly Property ProfileItemNode As Type = GetType(ProfileNodeItem)
|
307 |
|
308 |
#Region "IDisposable Support"
|
309 |
|
310 |
Protected Overrides Sub Dispose(disposing As Boolean)
|
311 |
If Not Me.disposedValue Then
|
312 |
If disposing Then
|
313 |
|
314 |
Call settingsData.Save()
|
315 |
End If
|
316 |
|
317 |
|
318 |
|
319 |
End If
|
320 |
Me.disposedValue = True
|
321 |
End Sub
|
322 |
#End Region
|
323 |
|
324 |
Protected Overrides Function __getDefaultPath() As String
|
325 |
Return FilePath
|
326 |
End Function
|
327 |
End Class
|
328 |
End Namespace
|