1 #Region "Microsoft.VisualBasic::955cb25b01fa63265ea9a9affab86884, Microsoft.VisualBasic.Core\ComponentModel\DataSource\Property\PropertyValue.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     '     Class PropertyValue
35     
36     '         Properties: obj, value
37     
38     '         Constructor: (+2 OverloadsSub New
39     
40     '         Function: [New], GetValue, (+2 Overloads) Read, SetValue, ToString
41     
42     '         SubSetValue
43     
44     
45     ' /********************************************************************************/
46
47 #End Region
48
49 Imports System.Reflection
50 Imports Microsoft.VisualBasic.Language
51 Imports Microsoft.VisualBasic.Serialization.JSON
52
53 Namespace ComponentModel.DataSourceModel
54
55     ''' <summary>
56     ''' The <see cref="PropertyInfo"/> like definition of the extension property.
57     ''' </summary>
58     ''' <typeparam name="T"></typeparam>
59     Public Class PropertyValue(Of T) : Inherits Value(Of T)
60
61         ReadOnly __get As Func(Of T)
62         ReadOnly __set As Action(Of T)
63
64         ''' <summary>
65         ''' The Extension property value.
66         ''' </summary>
67         ''' <returns></returns>
68         Public Overrides Property value As T
69             Get
70                 Return __get()
71             End Get
72             Set(value As T)
73                 MyBase.value = value
74
75                 If Not __set Is Nothing Then
76                     Call __set(value)  ' 因为在初始化的时候会对这个属性赋值,但是set没有被初始化,所以会出错,在这里加了一个if判断来避免空引用的错误
77                 End If
78             End Set
79         End Property
80
81         ''' <summary>
82         ''' The instance object for this extension property
83         ''' </summary>
84         ''' <returns></returns>
85         Public Property obj As IClassObject
86
87         ''' <summary>
88         ''' Custom property value.(value generated based on the extension property host <see cref="obj"/>)
89         ''' </summary>
90         ''' <param name="[get]">请勿使用<see cref="GetValue"/></param>函数,否则会出现栈空间溢出
91         ''' <param name="[set]">请勿使用<see cref="SetValue"/></param>方法,否则会出现栈空间溢出
92         Sub New([get] As Func(Of T), [set] As Action(Of T))
93             __get = [get]
94             __set = [set]
95         End Sub
96
97         ''' <summary>
98         ''' Tag property value.(默认是将数据写入到基本类型的值之中)
99         ''' </summary>
100         Sub New()
101             __get = Function() MyBase.value
102             __set = Sub(v) MyBase.value = v
103         End Sub
104
105         ''' <summary>
106         ''' 这个主要是应用于Linq表达式之中,将属性值设置之后返回宿主对象实例
107         ''' </summary>
108         ''' <param name="value"></param>
109         ''' <returns></returns>
110         Public Function SetValue(value As T) As IClassObject
111             Call __set(value)
112             Return obj
113         End Function
114
115         ''' <summary>
116         ''' Property Get Value
117         ''' </summary>
118         ''' <param name="x"></param>
119         ''' <returns></returns>
120         Public Overloads Shared Narrowing Operator CType(x As PropertyValue(Of T)) As T
121             Return x.value
122         End Operator
123
124         ''' <summary>
125         ''' <see cref="Value"/> -> <see cref="GetObjectJson"/>
126         ''' </summary>
127         ''' <returns></returns>
128         Public Overrides Function ToString() As String
129             Return value.GetJson
130         End Function
131
132         Public Shared Function GetValue(Of Cls As IClassObject)(x As Cls, name As StringAs PropertyValue(Of T)
133             Dim value As Object = x.Extension.DynamicHashTable(name)
134             Dim pv As PropertyValue(Of T) = DirectCast(value, PropertyValue(Of T))
135             Return pv
136         End Function
137
138         Public Shared Sub SetValue(Of Cls As IClassObject)(x As Cls, name As String, value As T)
139             Dim pvo As Object = x.Extension.DynamicHashTable(name)
140             Dim pv As PropertyValue(Of T) = DirectCast(pvo, PropertyValue(Of T))
141             pv.value = value
142         End Sub
143
144         ''' <summary>
145         ''' Creates a new extension property for the target <see cref="BaseClass"/>
146         ''' </summary>
147         ''' <typeparam name="Cls"></typeparam>
148         ''' <param name="x"></param>
149         ''' <param name="name"></param>
150         ''' <returns></returns>
151         Public Shared Function [New](Of Cls As IClassObject)(x As Cls, name As StringAs PropertyValue(Of T)
152             Dim value As New PropertyValue(Of T)()
153             x.Extension.DynamicHashTable(name) = value
154             value.obj = x
155             Return value
156         End Function
157
158         ''' <summary>
159         ''' Gets the tag property value from the <see cref="BaseClass"/>.(读取<see cref="BaseClass"/>对象之中的一个拓展属性)
160         ''' </summary>
161         ''' <typeparam name="Cls"></typeparam>
162         ''' <param name="x"></param>
163         ''' <param name="name"></param>
164         ''' <returns></returns>
165         Public Shared Function Read(Of Cls As IClassObject)(x As Cls, name As StringAs PropertyValue(Of T)
166             If x.Extension Is Nothing Then
167                 x.Extension = New ExtendedProps
168             End If
169             Dim prop As Object = x.Extension.DynamicHashTable(name)
170             If prop Is Nothing Then
171                 prop = PropertyValue(Of T).[New](Of Cls)(x, name)
172             End If
173             Return DirectCast(prop, PropertyValue(Of T))
174         End Function
175
176         Public Shared Function Read(Of Cls As IClassObject)(x As Cls, pm As MethodBase) As PropertyValue(Of T)
177             Return Read(Of Cls)(x, pm.Name)
178         End Function
179     End Class
180 End Namespace