| 1 | #Region "Microsoft.VisualBasic::fceb661b4855cbad2b423e08335264b3, Microsoft.VisualBasic.Core\ComponentModel\DataSource\SchemaMaps\BindProperty(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 BindProperty |
| 35 | ' |
| 36 | ' Properties: Identity, IsNull, IsPrimitive, Type |
| 37 | ' |
| 38 | ' Constructor: (+4 Overloads) Sub New |
| 39 | ' |
| 40 | ' Function: FromSchemaTable, GetValue, ToString |
| 41 | ' |
| 42 | ' Sub: SetValue |
| 43 | ' |
| 44 | ' |
| 45 | ' /********************************************************************************/ |
| 46 | |
| 47 | #End Region |
| 48 | |
| 49 | Imports System.Reflection |
| 50 | Imports System.Runtime.CompilerServices |
| 51 | Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic |
| 52 | Imports Microsoft.VisualBasic.Emit.Delegates |
| 53 | Imports Microsoft.VisualBasic.Serialization |
| 54 | |
| 55 | Namespace ComponentModel.DataSourceModel.SchemaMaps |
| 56 | |
| 57 | ''' <summary> |
| 58 | ''' Schema for <see cref="Attribute"/> and its bind <see cref="PropertyInfo"/>/<see cref="FieldInfo"/> object target. |
| 59 | ''' (使用这个对象将公共的域或者属性的读写统一起来) |
| 60 | ''' </summary> |
| 61 | ''' <typeparam name="T"></typeparam> |
| 62 | Public Structure BindProperty(Of T As Attribute) |
| 63 | Implements IReadOnlyId |
| 64 | Implements INamedValue |
| 65 | Implements IProperty |
| 66 | |
| 67 | ''' <summary> |
| 68 | ''' The property/field object that bind with its custom attribute <see cref="field"/> of type <typeparamref name="T"/> |
| 69 | ''' </summary> |
| 70 | Dim member As MemberInfo |
| 71 | ''' <summary> |
| 72 | ''' The flag for this field binding. |
| 73 | ''' </summary> |
| 74 | Dim field As T |
| 75 | Dim name As String |
| 76 | |
| 77 | ReadOnly __setValue As Action(Of Object, Object) |
| 78 | ReadOnly __getValue As Func(Of Object, Object) |
| 79 | |
| 80 | #Region "Property List" |
| 81 | |
| 82 | ''' <summary> |
| 83 | ''' Gets the type of this <see cref="member"/>. |
| 84 | ''' </summary> |
| 85 | ''' <returns></returns> |
| 86 | Public ReadOnly Property Type As Type |
| 87 | |
| 88 | ''' <summary> |
| 89 | ''' The map name or the <see cref="PropertyInfo.Name"/>. |
| 90 | ''' (这个属性会首先查找标记的自定义属性的名称结果,如果不存在才会使用属性或者字段的反射成员名称) |
| 91 | ''' </summary> |
| 92 | ''' <returns></returns> |
| 93 | Public Property Identity As String Implements IReadOnlyId.Identity, INamedValue.Key |
| 94 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 95 | Get |
| 96 | If name.StringEmpty Then |
| 97 | name = member.Name |
| 98 | End If |
| 99 | |
| 100 | Return name |
| 101 | End Get |
| 102 | Friend Set(value As String) |
| 103 | name = value |
| 104 | End Set |
| 105 | End Property |
| 106 | |
| 107 | ''' <summary> |
| 108 | ''' Is this map data is null on its attribute or property data? |
| 109 | ''' </summary> |
| 110 | ''' <returns></returns> |
| 111 | Public ReadOnly Property IsNull As Boolean |
| 112 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 113 | Get |
| 114 | Return member Is Nothing OrElse field Is Nothing |
| 115 | End Get |
| 116 | End Property |
| 117 | |
| 118 | ''' <summary> |
| 119 | ''' Gets a value indicating whether the <see cref="System.Type"/> is one of the primitive types. |
| 120 | ''' </summary> |
| 121 | ''' <returns> |
| 122 | ''' true if the <see cref="System.Type"/> is one of the primitive types; otherwise, false. |
| 123 | ''' </returns> |
| 124 | Public ReadOnly Property IsPrimitive As Boolean |
| 125 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 126 | Get |
| 127 | Return Scripting.IsPrimitive(Type) |
| 128 | End Get |
| 129 | End Property |
| 130 | #End Region |
| 131 | |
| 132 | Sub New(attr As T, prop As PropertyInfo, Optional getName As IToString(Of T) = Nothing) |
| 133 | field = attr |
| 134 | member = prop |
| 135 | Type = prop.PropertyType |
| 136 | |
| 137 | If Not getName Is Nothing Then |
| 138 | name = getName(attr) |
| 139 | End If |
| 140 | |
| 141 | ' Compile the property get/set as the delegate |
| 142 | With prop |
| 143 | __setValue = AddressOf prop.SetValue ' .DeclaringType.PropertySet(.Name) |
| 144 | __getValue = AddressOf prop.GetValue ' .DeclaringType.PropertyGet(.Name) |
| 145 | End With |
| 146 | End Sub |
| 147 | |
| 148 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 149 | Sub New([property] As PropertyInfo) |
| 150 | Call Me.New(Nothing, [property]) |
| 151 | End Sub |
| 152 | |
| 153 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 154 | Sub New(field As FieldInfo) |
| 155 | Call Me.New(Nothing, field) |
| 156 | End Sub |
| 157 | |
| 158 | Sub New(attr As T, field As FieldInfo, Optional getName As IToString(Of T) = Nothing) |
| 159 | Me.field = attr |
| 160 | Me.member = field |
| 161 | Type = field.FieldType |
| 162 | |
| 163 | If Not getName Is Nothing Then |
| 164 | name = getName(attr) |
| 165 | End If |
| 166 | |
| 167 | With field |
| 168 | __setValue = AddressOf field.SetValue ' .DeclaringType.FieldSet(.Name) |
| 169 | __getValue = AddressOf field.GetValue ' .DeclaringType.FieldGet(.Name) |
| 170 | End With |
| 171 | End Sub |
| 172 | |
| 173 | ' Exceptions: |
| 174 | ' T:System.ArgumentException: |
| 175 | ' The index array does not contain the type of arguments needed.-or- The property's |
| 176 | ' set accessor is not found. -or-value cannot be converted to the type of System.Reflection.PropertyInfo.PropertyType. |
| 177 | ' ' T:System.Reflection.TargetException: |
| 178 | ' In the .NET for Windows Store apps or the Portable Class Library, catch System.Exception |
| 179 | ' instead.The object does not match the target type, or a property is an instance |
| 180 | ' property but obj is null. |
| 181 | ' ' T:System.Reflection.TargetParameterCountException: |
| 182 | ' The number of parameters in index does not match the number of parameters the |
| 183 | ' indexed property takes. |
| 184 | ' ' T:System.MethodAccessException: |
| 185 | ' In the .NET for Windows Store apps or the Portable Class Library, catch the base |
| 186 | ' class exception, System.MemberAccessException, instead.There was an illegal attempt |
| 187 | ' to access a private or protected method inside a class. |
| 188 | ' ' T:System.Reflection.TargetInvocationException: |
| 189 | ' An error occurred while setting the property value. For example, an index value |
| 190 | ' specified for an indexed property is out of range. The System.Exception.InnerException |
| 191 | ' property indicates the reason for the error. |
| 192 | |
| 193 | ''' <summary> |
| 194 | ''' Sets the property value of a specified object with optional index values for |
| 195 | ''' index properties. |
| 196 | ''' (这个设置值的函数只适用于``Class``类型,对于``Structure``类型而言,则无法正常的工作) |
| 197 | ''' </summary> |
| 198 | ''' <param name="obj">The object whose property value will be set.</param> |
| 199 | ''' <param name="value">The new property value.</param> |
| 200 | ''' |
| 201 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 202 | Public Sub SetValue(obj As Object, value As Object) Implements IProperty.SetValue |
| 203 | ' 2017-6-26 目前value参数为空值的话,会报错,故而在这里添加了一个If分支判断 |
| 204 | If value IsNot Nothing Then |
| 205 | Call __setValue(obj, value) |
| 206 | End If |
| 207 | End Sub |
| 208 | |
| 209 | ' Exceptions: |
| 210 | ' T:System.ArgumentException: |
| 211 | ' The index array does not contain the type of arguments needed.-or- The property's |
| 212 | ' get accessor is not found. |
| 213 | ' ' T:System.Reflection.TargetException: |
| 214 | ' In the .NET for Windows Store apps or the Portable Class Library, catch System.Exception |
| 215 | ' instead.The object does not match the target type, or a property is an instance |
| 216 | ' property but obj is null. |
| 217 | ' ' T:System.Reflection.TargetParameterCountException: |
| 218 | ' The number of parameters in index does not match the number of parameters the |
| 219 | ' indexed property takes. |
| 220 | ' ' T:System.MethodAccessException: |
| 221 | ' In the .NET for Windows Store apps or the Portable Class Library, catch the base |
| 222 | ' class exception, System.MemberAccessException, instead.There was an illegal attempt |
| 223 | ' to access a private or protected method inside a class. |
| 224 | ' ' T:System.Reflection.TargetInvocationException: |
| 225 | ' An error occurred while retrieving the property value. For example, an index |
| 226 | ' value specified for an indexed property is out of range. The System.Exception.InnerException |
| 227 | ' property indicates the reason for the error. |
| 228 | |
| 229 | ''' <summary> |
| 230 | ''' Returns the property value of a specified object with optional index values for |
| 231 | ''' indexed properties. |
| 232 | ''' </summary> |
| 233 | ''' <param name="x">The object whose property value will be returned.</param> |
| 234 | ''' <returns>The property value of the specified object.</returns> |
| 235 | ''' |
| 236 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 237 | Public Function GetValue(x As Object) As Object Implements IProperty.GetValue |
| 238 | Return __getValue(x) |
| 239 | End Function |
| 240 | |
| 241 | ''' <summary> |
| 242 | ''' Display this schema maps in Visualbasic style. |
| 243 | ''' </summary> |
| 244 | ''' <returns></returns> |
| 245 | Public Overrides Function ToString() As String |
| 246 | Return $"Dim {member.Name} As {Type.FullName}" |
| 247 | End Function |
| 248 | |
| 249 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 250 | Public Shared Function FromSchemaTable(x As KeyValuePair(Of T, PropertyInfo)) As BindProperty(Of T) |
| 251 | Return New BindProperty(Of T) With { |
| 252 | .field = x.Key, |
| 253 | .member = x.Value |
| 254 | } |
| 255 | End Function |
| 256 | End Structure |
| 257 | End Namespace |