1 | #Region "Microsoft.VisualBasic::3c9cd1d9c66ebfb506cd7bfaa565d4af, Microsoft.VisualBasic.Core\ComponentModel\DataSource\Property\NamedCollection(Of T).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 | ' Structure NamedCollection |
35 | ' |
36 | ' Properties: Description, IsEmpty, Length, Name, Value |
37 | ' |
38 | ' Constructor: (+4 Overloads) Sub New |
39 | ' Function: GetEnumerator, GetValues, IEnumerable_GetEnumerator, ToString |
40 | ' |
41 | ' |
42 | ' /********************************************************************************/ |
43 | |
44 | #End Region |
45 | |
46 | Imports System.Runtime.CompilerServices |
47 | Imports System.Xml.Serialization |
48 | Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic |
49 | Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel.Repository |
50 | Imports Microsoft.VisualBasic.Language |
51 | Imports Microsoft.VisualBasic.Language.Default |
52 | Imports Microsoft.VisualBasic.Linq |
53 | Imports Microsoft.VisualBasic.Text.Xml.Models |
54 | |
55 | Namespace ComponentModel.DataSourceModel |
56 | |
57 | REM 2017-7-18 Add(T) not working on the Add(Object)??? |
58 | |
59 | ' System.InvalidOperationException: 要使 XML 可序列化,从 IEnumerable 继承的类型必须在继承层次结构的所有级别上实现 Add(System.Object)。Microsoft.VisualBasic.ComponentModel.DataSourceModel.NamedCollection`1[[Biodeep.Reference.Library.mysql.data_lc_ms_metadb, |
60 | ' Biodeep.Reference.Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] 不支持实现 Add(System.Object)。 |
61 | ' 在 System.Xml.Serialization.TypeScope.GetEnumeratorElementType(Type type, TypeFlags& flags) |
62 | ' 在 System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference) |
63 | ' 在 System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError) |
64 | ' 在 System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference) |
65 | ' 在 System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError) |
66 | ' 在 System.Xml.Serialization.ModelScope.GetTypeModel(Type type, Boolean directReference) |
67 | ' 在 System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace) |
68 | ' 在 System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace) |
69 | ' 在 System.Xml.Serialization.XmlSerializer..ctor(Type type) |
70 | ' 在 Microsoft.VisualBasic.XmlExtensions.GetXml(Object obj, Type type, Boolean throwEx, XmlEncodings xmlEncoding) 位置 D:\GCModeller\src\runtime\sciBASIC#\Microsoft.VisualBasic.Architecture.Framework\Extensions\Doc\XmlExtensions.vb:行号 193 |
71 | ' --- 内部异常堆栈跟踪的结尾 --- |
72 | ' 在 Microsoft.VisualBasic.XmlExtensions.GetXml(Object obj, Type type, Boolean throwEx, XmlEncodings xmlEncoding) 位置 D:\GCModeller\src\runtime\sciBASIC#\Microsoft.VisualBasic.Architecture.Framework\Extensions\Doc\XmlExtensions.vb:行号 207 |
73 | ' 在 Microsoft.VisualBasic.XmlExtensions.GetXml[T](T obj, Boolean ThrowEx, XmlEncodings xmlEncoding) 位置 D:\GCModeller\src\runtime\sciBASIC#\Microsoft.VisualBasic.Architecture.Framework\Extensions\Doc\XmlExtensions.vb:行号 162 |
74 | ' 在 Biodeep.Reference.Library.Build.CLI.Grouping(CommandLine args) 位置 D:\smartnucl_integrative\biodeepDB\Build.Ref\CLI\Database.vb:行号 42 |
75 | ' --- 内部异常堆栈跟踪的结尾 --- |
76 | ' 在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) |
77 | ' 在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) |
78 | ' 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) |
79 | ' 在 System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) |
80 | ' 在 Microsoft.VisualBasic.CommandLine.Reflection.EntryPoints.APIEntryPoint.__directInvoke(Object[] callParameters, Object target, Boolean Throw) 位置 D:\GCModeller\src\runtime\sciBASIC#\Microsoft.VisualBasic.Architecture.Framework\CommandLine\Reflection\EntryPoints\APIEntryPoint.vb:行号 209 |
81 | ' --- 内部异常堆栈跟踪的结尾 --- |
82 | |
83 | ''' <summary> |
84 | ''' The value object collection that have a name string, using <see cref="NamedVector(Of T)"/> |
85 | ''' for XML serialization instead of using this data model. |
86 | ''' </summary> |
87 | ''' <typeparam name="T"></typeparam> |
88 | Public Structure NamedCollection(Of T) : Implements INamedValue |
89 | Implements IKeyValuePairObject(Of String, T()) |
90 | Implements Value(Of T()).IValueOf |
91 | Implements IEnumerable(Of T) |
92 | Implements IGrouping(Of String, T) |
93 | Implements IsEmpty |
94 | |
95 | ''' <summary> |
96 | ''' 这个集合对象的标识符名称 |
97 | ''' </summary> |
98 | ''' <returns></returns> |
99 | <XmlAttribute> Public Property Name As String Implements _ |
100 | IKeyedEntity(Of String).Key, |
101 | IKeyValuePairObject(Of String, T()).Key, |
102 | IGrouping(Of String, T).Key |
103 | |
104 | ''' <summary> |
105 | ''' 目标集合对象 |
106 | ''' </summary> |
107 | ''' <returns></returns> |
108 | Public Property Value As T() Implements IKeyValuePairObject(Of String, T()).Value, Value(Of T()).IValueOf.Value |
109 | |
110 | ''' <summary> |
111 | ''' 目标集合对象的描述信息 |
112 | ''' </summary> |
113 | ''' <returns></returns> |
114 | Public Property Description As String |
115 | |
116 | ''' <summary> |
117 | ''' 当前的这个命名的目标集合对象是否是空对象? |
118 | ''' </summary> |
119 | ''' <returns></returns> |
120 | Public ReadOnly Property IsEmpty As Boolean Implements IsEmpty.IsEmpty |
121 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
122 | Get |
123 | Return Name Is Nothing AndAlso Value Is Nothing |
124 | End Get |
125 | End Property |
126 | |
127 | Default Public Property Item(index As Integer) As T |
128 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
129 | Get |
130 | Return Value(index) |
131 | End Get |
132 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
133 | Set(value As T) |
134 | Me.Value(index) = value |
135 | End Set |
136 | End Property |
137 | |
138 | ''' <summary> |
139 | ''' <see cref="Value"/> array length |
140 | ''' </summary> |
141 | ''' <returns></returns> |
142 | Public ReadOnly Property Length As Integer |
143 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
144 | Get |
145 | Return Value.Length |
146 | End Get |
147 | End Property |
148 | |
149 | ''' <summary> |
150 | ''' |
151 | ''' </summary> |
152 | ''' <param name="source">名称属性<see cref="NamedValue(Of T).Name"/></param>必须是相同的 |
153 | Sub New(source As IEnumerable(Of NamedValue(Of T))) |
154 | Dim array = source.ToArray |
155 | |
156 | Name = array(Scan0).Name |
157 | Value = array.Select(Function(x) x.Value).ToArray |
158 | Description = array _ |
159 | .Select(Function(x) x.Description) _ |
160 | .Where(Function(s) Not s.StringEmpty) _ |
161 | .Distinct _ |
162 | .JoinBy("; ") |
163 | End Sub |
164 | |
165 | Sub New(xmlNode As NamedVector(Of T)) |
166 | With xmlNode |
167 | Name = .name |
168 | Value = .vector |
169 | End With |
170 | End Sub |
171 | |
172 | Sub New(name$, data As IEnumerable(Of T), Optional description$ = Nothing) |
173 | Call Me.New(name, data.SafeQuery.ToArray, description) |
174 | End Sub |
175 | |
176 | Sub New(name$, value As T(), Optional description$ = Nothing) |
177 | Me.Name = name |
178 | Me.Description = description |
179 | Me.Value = value |
180 | End Sub |
181 | |
182 | Public Function GetValues() As NamedValue(Of T)() |
183 | Dim name$ = Me.Name |
184 | Dim describ$ = Description |
185 | |
186 | Return Value _ |
187 | .Select(Function(v) |
188 | Return New NamedValue(Of T) With { |
189 | .Name = name, |
190 | .Description = describ, |
191 | .Value = v |
192 | } |
193 | End Function) _ |
194 | .ToArray |
195 | End Function |
196 | |
197 | Public Overrides Function ToString() As String |
198 | Return Name |
199 | End Function |
200 | |
201 | Public Iterator Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator |
202 | For Each x As T In Value.SafeQuery |
203 | Yield x |
204 | Next |
205 | End Function |
206 | |
207 | Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator |
208 | Yield GetEnumerator() |
209 | End Function |
210 | |
211 | Public Shared Widening Operator CType(tuple As (name$, value As T())) As NamedCollection(Of T) |
212 | Return New NamedCollection(Of T)(tuple.name, tuple.value) |
213 | End Operator |
214 | End Structure |
215 | End Namespace |