1 |
#Region "Microsoft.VisualBasic::56c40fd5bb21d327903fc780f47e2aa5, Microsoft.VisualBasic.Core\Scripting\Actives.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 |
#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 |
|
55 |
|
56 |
|
57 |
Public Module Activity
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
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 |
|
92 |
|
93 |
|
94 |
|
95 |
|
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 |
|
103 |
与<see cref="Activator.CreateInstance(Type)"/>所不同的是,这个函数还会对属性进行一些Demo值得赋值操作
|
104 |
|
105 |
|
106 |
|
107 |
<Extension> Public Function ActiveObject(type As Type) As Object
|
108 |
Return type.__active
|
109 |
End Function
|
110 |
|
111 |
|
112 |
Creates the example instance object for the example
|
113 |
|
114 |
|
115 |
|
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 |
|
168 |
+ <see cref="System.Collections.Generic.List(Of T)"/>
|
169 |
+ <see cref="Microsoft.VisualBasic.Language.List(Of T)"/>(会统一返回这种类型)
|
170 |
|
171 |
|
172 |
|
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
|