1 |
#Region "Microsoft.VisualBasic::7dea8c94f2219af62b1b9ba3eaa259df, Microsoft.VisualBasic.Core\Text\Xml\Models\ValueTuples\ValuePair.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.Xml.Serialization
|
52 |
Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic
|
53 |
|
54 |
Namespace Text.Xml.Models
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
<XmlType("hashEntry")> Public Class KeyValuePair
|
64 |
Implements INamedValue
|
65 |
Implements IKeyValuePair
|
66 |
|
67 |
Public Sub New()
|
68 |
|
69 |
End Sub
|
70 |
|
71 |
Sub New(item As KeyValuePair(Of String, String))
|
72 |
Key = item.Key
|
73 |
Value = item.Value
|
74 |
End Sub
|
75 |
|
76 |
Sub New(Key As String, Value As String)
|
77 |
Me.Key = Key
|
78 |
Me.Value = Value
|
79 |
End Sub
|
80 |
|
81 |
|
82 |
Defines a key/value pair that can be set or retrieved.(特化的<see cref="IKeyValuePairObject(Of String, String)"></see>字符串属性类型)
|
83 |
|
84 |
</remarks>
|
85 |
Public Interface IKeyValuePair : Inherits IKeyValuePairObject(Of String, String)
|
86 |
End Interface
|
87 |
|
88 |
#Region "ComponentModel.Collection.Generic.KeyValuePairObject(Of String, String) property overrides"
|
89 |
|
90 |
|
91 |
Gets the key in the key/value pair.
|
92 |
|
93 |
<value></value>
|
94 |
<returns></returns>
|
95 |
在这里可能用不了<see cref="XmlAttributeAttribute"></see>自定义属性,因为其基本类型之中的Key和Value可以是任意的类型的,Attribute格式无法序列化复杂的数据类型</remarks>
|
96 |
<XmlAttribute> Public Property Key As String Implements INamedValue.Key, IKeyValuePair.Key
|
97 |
<XmlAttribute> Public Property Value As String Implements IKeyValuePairObject(Of String, String).Value
|
98 |
#End Region
|
99 |
|
100 |
Public Overloads Shared Widening Operator CType(obj As KeyValuePair(Of String, String)) As KeyValuePair
|
101 |
Return New KeyValuePair With {
|
102 |
.Key = obj.Key,
|
103 |
.Value = obj.Value
|
104 |
}
|
105 |
End Operator
|
106 |
|
107 |
Public Overloads Shared Widening Operator CType(obj As String()) As KeyValuePair
|
108 |
Return New KeyValuePair With {
|
109 |
.Key = obj.First,
|
110 |
.Value = obj.ElementAtOrDefault(1)
|
111 |
}
|
112 |
End Operator
|
113 |
|
114 |
Public Overrides Function ToString() As String
|
115 |
Return String.Format("{0} -> {1}", Key, Value)
|
116 |
End Function
|
117 |
|
118 |
Public Overloads Shared Function CreateObject(key As String, value As String) As KeyValuePair
|
119 |
Return New KeyValuePair With {
|
120 |
.Key = key,
|
121 |
.Value = value
|
122 |
}
|
123 |
End Function
|
124 |
|
125 |
Public Shared Function ToDictionary(list As IEnumerable(Of KeyValuePair)) As Dictionary(Of String, String)
|
126 |
Dim Dictionary As Dictionary(Of String, String) =
|
127 |
list.ToDictionary(
|
128 |
Function(obj) obj.Key,
|
129 |
Function(obj) obj.Value)
|
130 |
Return Dictionary
|
131 |
End Function
|
132 |
|
133 |
Public Overrides Function Equals(obj As Object) As Boolean
|
134 |
If TypeOf obj Is KeyValuePair Then
|
135 |
Dim KeyValuePair As KeyValuePair = DirectCast(obj, KeyValuePair)
|
136 |
|
137 |
Return String.Equals(KeyValuePair.Key, Key) AndAlso
|
138 |
String.Equals(KeyValuePair.Value, Value)
|
139 |
Else
|
140 |
Return False
|
141 |
End If
|
142 |
End Function
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
<param name="obj"></param>
|
148 |
<param name="strict">If strict is TRUE then the function of the string compares will case sensitive.</param>
|
149 |
<returns></returns>
|
150 |
</remarks>
|
151 |
Public Overloads Function Equals(obj As KeyValuePair, Optional strict As Boolean = True) As Boolean
|
152 |
If strict Then
|
153 |
Return String.Equals(obj.Key, Key) AndAlso String.Equals(obj.Value, Value)
|
154 |
Else
|
155 |
Return String.Equals(obj.Key, Key, StringComparison.OrdinalIgnoreCase) AndAlso
|
156 |
String.Equals(obj.Value, Value, StringComparison.OrdinalIgnoreCase)
|
157 |
End If
|
158 |
End Function
|
159 |
|
160 |
Public Shared Function Distinct(source As KeyValuePair()) As KeyValuePair()
|
161 |
Dim List = (From obj In source Select obj Order By obj.Key Ascending).AsList
|
162 |
|
163 |
For i As Integer = 0 To List.Count - 1
|
164 |
If i >= List.Count Then
|
165 |
Exit For
|
166 |
End If
|
167 |
Dim item = List(i)
|
168 |
|
169 |
For j As Integer = i + 1 To List.Count - 1
|
170 |
If j >= List.Count Then
|
171 |
Exit For
|
172 |
End If
|
173 |
If item.Equals(List(j)) Then
|
174 |
Call List.RemoveAt(j)
|
175 |
j -= 1
|
176 |
End If
|
177 |
Next
|
178 |
Next
|
179 |
|
180 |
Return List.ToArray
|
181 |
End Function
|
182 |
End Class
|
183 |
End Namespace
|