1 #Region "Microsoft.VisualBasic::56c40fd5bb21d327903fc780f47e2aa5, Microsoft.VisualBasic.Core\Scripting\Actives.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     '     Module Activity
35     
36     '         Function: __active, __activeArray, __activeDictionary, __activeList, Active
37     '                   ActiveObject, DisplayType
38     
39     
40     ' /********************************************************************************/
41
42 #End Region
43
44 Imports System.Reflection
45 Imports System.Runtime.CompilerServices
46 Imports System.Text
47 Imports Microsoft.VisualBasic.CommandLine.Reflection
48 Imports Microsoft.VisualBasic.Serialization.JSON
49 Imports Microsoft.VisualBasic.Language
50 Imports v = System.Array
51
52 Namespace Scripting
53
54     ''' <summary>
55     ''' Display the object instance object json from the type definition by using <see cref="Activator"/>.
56     ''' </summary>
57     Public Module Activity
58
59         ''' <summary>
60         ''' 请注意,所需要进行显示的类型必须要Public类型的,假若该目标类型在Module之中,请保证Module也是Public访问的
61         ''' </summary>
62         ''' <param name="type"></param>
63         ''' <returns></returns>
64         <Extension> Public Function DisplayType(type As Type) As String
65             Dim sb As New StringBuilder
66             Dim view As ActiveViews = type.GetCustomAttribute(Of ActiveViews)
67             Dim fullName$ = type.FullName
68
69             If fullName.Split("."c).First = "System" Then
70                 Call sb.AppendLine($"**Decalre**:  _<a href=""https://msdn.microsoft.com/en-us/library/{fullName.ToLower}(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1"">{fullName}</a>_")
71             Else
72                 Call sb.AppendLine($"**Decalre**:  _{fullName}_")
73             End If
74
75             Call sb.AppendLine()
76             Call sb.AppendLine("Example: ")
77
78             If view Is Nothing Then
79                 Call sb.AppendLine("```json")
80                 Call sb.AppendLine(Active(type))
81                 Call sb.AppendLine("```")
82             Else
83                 Call sb.AppendLine("```" & view.type)
84                 Call sb.AppendLine(view.Views)
85                 Call sb.AppendLine("```")
86             End If
87
88             Return sb.ToString
89         End Function
90
91         ''' <summary>
92         ''' Display the json of the target type its instance object.
93         ''' </summary>
94         ''' <param name="type"></param>
95         ''' <returns></returns>
96         ''' 
97         <MethodImpl(MethodImplOptions.AggressiveInlining)>
98         Public Function Active(type As Type) As String
99             Return type.GetObjectJson(obj:=type.__active)
100         End Function
101
102         ''' <summary>
103         ''' 与<see cref="Activator.CreateInstance(Type)"/>所不同的是,这个函数还会对属性进行一些Demo值得赋值操作
104         ''' </summary>
105         ''' <param name="type"></param>
106         ''' <returns></returns>
107         <Extension> Public Function ActiveObject(type As Type) As Object
108             Return type.__active
109         End Function
110
111         ''' <summary>
112         ''' Creates the example instance object for the example
113         ''' </summary>
114         ''' <param name="type"></param>
115         ''' <returns></returns>
116         <Extension> Private Function __active(type As Type) As Object
117             If type.Equals(GetType(String)) Then
118                 Return type.FullName
119             End If
120             If type.Equals(GetType(Char)) Then
121                 Return "c"c
122             End If
123             If type.Equals(GetType(Date)) OrElse
124                 type.Equals(GetType(DateTime)) Then
125                 Return Now
126             End If
127             If __examples.ContainsKey(type) Then
128                 Return __examples(type)
129             End If
130
131             If type.IsInheritsFrom(GetType(Array)) Then
132                 Return type.__activeArray
133             ElseIf type.IsInheritsFrom(GetType(List(Of )), strict:=False) Then
134                 Return type.__activeList
135             ElseIf type.IsInheritsFrom(GetType(Dictionary(Of ,)), strict:=False) Then
136                 Return type.__activeDictionary
137             End If
138
139             Try
140                 Dim obj As Object = Activator.CreateInstance(type)
141                 Dim source As IEnumerable(Of PropertyInfo) =
142                     type _
143                     .GetProperties _
144                     .Where(Function(x)
145                                Return x.CanWrite AndAlso
146                                       x.GetIndexParameters _
147                                        .IsNullOrEmpty
148                            End Function)
149
150                 For Each prop As PropertyInfo In source
151                     Dim value As Object = prop _
152                         .PropertyType _
153                         .__active
154
155                     ' 对于复杂的自定义类型,进行递归分解构造
156                     Call prop.SetValue(obj, value)
157                 Next
158
159                 Return obj
160             Catch ex As Exception
161                 ex = New Exception(type.FullName, ex)
162                 Call App.LogException(ex)
163                 Return Nothing
164             End Try
165         End Function
166
167         ''' <summary>
168         ''' + <see cref="System.Collections.Generic.List(Of T)"/>
169         ''' + <see cref="Microsoft.VisualBasic.Language.List(Of T)"/>(会统一返回这种类型)
170         ''' </summary>
171         ''' <param name="type"></param>
172         ''' <returns></returns>
173         <Extension>
174         Private Function __activeList(type As Type) As Object
175             Dim base As Type = type.GenericTypeArguments(Scan0)
176             Dim value As Object = base.__active
177             Dim list As Type = GetType(List(Of )).MakeGenericType(base)
178             Dim IList As IList = DirectCast(Activator.CreateInstance(list), IList)
179             Call IList.Add(value)
180             Return IList
181         End Function
182
183         <Extension>
184         Private Function __activeArray(type As Type) As Object
185             Dim base As Type = type.GetElementType
186             Dim value As Object = base.__active
187             Dim array As Array = v.CreateInstance(base, 1)
188             Call array.SetValue(value, Scan0)
189             Return array
190         End Function
191
192         <Extension>
193         Private Function __activeDictionary(type As Type) As Object
194             With type.GenericTypeArguments.AsList
195                 Dim baseKey As Type = .Item(0)
196                 Dim baseValue As Type = .Item(1)
197                 Dim k As Object = baseKey.__active
198                 Dim v As Object = baseValue.__active
199                 Dim IDictionary As IDictionary = DirectCast(Activator.CreateInstance(type), IDictionary)
200                 Call IDictionary.Add(k, v)
201                 Return IDictionary
202             End With
203         End Function
204
205         ReadOnly __examples As IReadOnlyDictionary(Of Type, Object) =
206             New Dictionary(Of Type, Object) From {
207  _
208                 {GetType(Double), 0R},
209                 {GetType(Double?), 0R},
210                 {GetType(Single), 0!},
211                 {GetType(Single?), 0!},
212                 {GetType(Integer), 0},
213                 {GetType(Integer?), 0},
214                 {GetType(Long), 0L},
215                 {GetType(Long?), 0L},
216                 {GetType(Short), 0S},
217                 {GetType(Short?), 0S},
218                 {GetType(Byte), 0},
219                 {GetType(SByte), 0},
220                 {GetType(Boolean), True},
221                 {GetType(Decimal), 0@}
222         }
223     End Module
224 End Namespace