1 |
#Region "Microsoft.VisualBasic::214000639240330917176737d0a703cb, Microsoft.VisualBasic.Core\Extensions\CodeDOM\CodeDOMExpressions.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 |
|
43 |
|
44 |
|
45 |
|
46 |
#End Region
|
47 |
|
48 |
Imports System.CodeDom
|
49 |
Imports System.Reflection
|
50 |
Imports System.Runtime.CompilerServices
|
51 |
Imports Microsoft.VisualBasic.Language
|
52 |
|
53 |
Namespace Emit.CodeDOM_VBC
|
54 |
|
55 |
Public Module CodeDOMExpressions
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
Public ReadOnly Property EntryPoint As CodeDom.CodeMemberMethod
|
62 |
Get
|
63 |
Dim Func As New CodeMemberMethod With {
|
64 |
.Name = "Main",
|
65 |
.ReturnType = Type(Of Integer)(),
|
66 |
.Attributes = MemberAttributes.Public Or MemberAttributes.Static
|
67 |
}
|
68 |
|
69 |
Func.Parameters.Add(Argument(Of String())("Argvs"))
|
70 |
|
71 |
Return Func
|
72 |
End Get
|
73 |
End Property
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
Or declare a method in a standard Module type.
|
82 |
|
83 |
Public Const PublicShared As MemberAttributes =
|
84 |
CodeDom.MemberAttributes.Public Or
|
85 |
CodeDom.MemberAttributes.Static
|
86 |
|
87 |
|
88 |
声明一个函数
|
89 |
|
90 |
<param name="name"></param>
|
91 |
<param name="args"></param>
|
92 |
<param name="returns"></param>
|
93 |
<param name="control"></param>
|
94 |
|
95 |
Public Function DeclareFunc(name As String,
|
96 |
args As Dictionary(Of String, Type),
|
97 |
returns As Type,
|
98 |
Optional control As CodeDom.MemberAttributes = PublicShared) As CodeDom.CodeMemberMethod
|
99 |
|
100 |
Dim Func As New CodeDom.CodeMemberMethod With {
|
101 |
.Name = name,
|
102 |
.ReturnType = returns.TypeRef,
|
103 |
.Attributes = control
|
104 |
}
|
105 |
|
106 |
If Not args.IsNullOrEmpty Then
|
107 |
For Each x In args
|
108 |
Call Func.Parameters.Add(Argument(x.Key, x.Value))
|
109 |
Next
|
110 |
End If
|
111 |
|
112 |
Return Func
|
113 |
End Function
|
114 |
|
115 |
|
116 |
|
117 |
Dim Name As <Type>
|
118 |
|
119 |
|
120 |
Declare a field in the type
|
121 |
|
122 |
<param name="Name"></param>
|
123 |
<param name="Type"></param>
|
124 |
|
125 |
Public Function Field(Name As String, Type As Type) As CodeDom.CodeMemberField
|
126 |
Return New CodeDom.CodeMemberField(name:=Name, type:=New CodeDom.CodeTypeReference(Type))
|
127 |
End Function
|
128 |
|
129 |
|
130 |
Reference of ``Me.Field``
|
131 |
|
132 |
<param name="Name"></param>
|
133 |
|
134 |
Public Function FieldRef(Name As String) As CodeDom.CodeFieldReferenceExpression
|
135 |
Return New CodeDom.CodeFieldReferenceExpression(New CodeDom.CodeThisReferenceExpression, Name)
|
136 |
End Function
|
137 |
|
138 |
Public Function Field(Name As String, type As String) As CodeDom.CodeFieldReferenceExpression
|
139 |
Return New CodeDom.CodeFieldReferenceExpression(New CodeDom.CodeTypeReferenceExpression(type), Name)
|
140 |
End Function
|
141 |
|
142 |
Public Function Comments(text As String) As CodeDom.CodeCommentStatement
|
143 |
Return New CodeDom.CodeCommentStatement(text)
|
144 |
End Function
|
145 |
|
146 |
|
147 |
Class object instance constructor
|
148 |
|
149 |
<param name="Type"></param>
|
150 |
<param name="parameters"></param>
|
151 |
|
152 |
Public Function [New](Type As Type, parameters As CodeDom.CodeExpression()) As CodeDom.CodeObjectCreateExpression
|
153 |
Return New CodeDom.CodeObjectCreateExpression(New CodeDom.CodeTypeReference(Type), parameters)
|
154 |
End Function
|
155 |
|
156 |
Public Function [New](type As String) As CodeDom.CodeObjectCreateExpression
|
157 |
Dim typeRef As New CodeDom.CodeTypeReference(type)
|
158 |
Return New CodeDom.CodeObjectCreateExpression(typeRef, {})
|
159 |
End Function
|
160 |
|
161 |
|
162 |
Class object instance constructor.
|
163 |
|
164 |
<typeparam name="T"></typeparam>
|
165 |
<param name="parameters"></param>
|
166 |
|
167 |
Public Function [New](Of T As Class)(parameters As Object()) As CodeDom.CodeObjectCreateExpression
|
168 |
If parameters.IsNullOrEmpty Then
|
169 |
Return [New](GetType(T), {})
|
170 |
Else
|
171 |
Dim args As CodePrimitiveExpression() = LinqAPI.Exec(Of CodePrimitiveExpression) <=
|
172 |
From obj As Object
|
173 |
In parameters
|
174 |
Select New CodePrimitiveExpression(obj)
|
175 |
|
176 |
Return [New](GetType(T), args)
|
177 |
End If
|
178 |
End Function
|
179 |
|
180 |
|
181 |
New object
|
182 |
|
183 |
<param name="typeRef"></param>
|
184 |
<param name="parameters"></param>
|
185 |
|
186 |
Public Function [New](typeRef As String, parameters As CodeDom.CodeExpression()) As CodeDom.CodeObjectCreateExpression
|
187 |
Dim objectType As New CodeDom.CodeTypeReference(typeRef)
|
188 |
If parameters Is Nothing Then
|
189 |
parameters = New CodeDom.CodeExpression() {}
|
190 |
End If
|
191 |
|
192 |
Return New CodeDom.CodeObjectCreateExpression(objectType, parameters)
|
193 |
End Function
|
194 |
|
195 |
|
196 |
声明一个局部变量
|
197 |
|
198 |
<param name="Name"></param>
|
199 |
<param name="Type"></param>
|
200 |
<param name="initExpression"></param>
|
201 |
|
202 |
Public Function LocalsInit(Name As String, Type As System.Type, Optional initExpression As CodeDom.CodeExpression = Nothing) As CodeDom.CodeVariableDeclarationStatement
|
203 |
Dim expr As New CodeDom.CodeVariableDeclarationStatement(New CodeDom.CodeTypeReference(Type), Name)
|
204 |
If Not initExpression Is Nothing Then
|
205 |
expr.InitExpression = initExpression
|
206 |
End If
|
207 |
Return expr
|
208 |
End Function
|
209 |
|
210 |
Public Function LocalsInit(Name As String, Type As String, Optional initExpression As CodeDom.CodeExpression = Nothing) As CodeDom.CodeVariableDeclarationStatement
|
211 |
Dim typeRef As New CodeDom.CodeTypeReference(Type)
|
212 |
Dim Expr = New CodeDom.CodeVariableDeclarationStatement(typeRef, Name)
|
213 |
If Not initExpression Is Nothing Then
|
214 |
Expr.InitExpression = initExpression
|
215 |
End If
|
216 |
Return Expr
|
217 |
End Function
|
218 |
|
219 |
|
220 |
Declare a local variable.
|
221 |
|
222 |
<param name="Name"></param>
|
223 |
<param name="Type"></param>
|
224 |
<param name="init"></param>
|
225 |
|
226 |
Public Function LocalsInit(Name As String, Type As System.Type, Optional init As Object = Nothing) As CodeDom.CodeVariableDeclarationStatement
|
227 |
If Not init Is Nothing Then
|
228 |
Return LocalsInit(Name, Type, New CodeDom.CodePrimitiveExpression(init))
|
229 |
Else
|
230 |
Return LocalsInit(Name, Type, initExpression:=Nothing)
|
231 |
End If
|
232 |
End Function
|
233 |
|
234 |
|
235 |
Ctype
|
236 |
|
237 |
<param name="obj"></param>
|
238 |
<param name="type"></param>
|
239 |
|
240 |
|
241 |
<Extension>
|
242 |
Public Function [CType](obj As CodeDom.CodeExpression, type As Type) As CodeDom.CodeCastExpression
|
243 |
Return New CodeDom.CodeCastExpression(New CodeDom.CodeTypeReference(type), obj)
|
244 |
End Function
|
245 |
|
246 |
|
247 |
Call method
|
248 |
|
249 |
<param name="Method"></param>
|
250 |
<param name="Parameters"></param>
|
251 |
<param name="obj"></param>
|
252 |
|
253 |
<Extension>
|
254 |
Public Function [Call](Method As MethodInfo,
|
255 |
Parameters As CodeDom.CodeExpression(),
|
256 |
Optional obj As CodeDom.CodeExpression = Nothing) As CodeDom.CodeMethodInvokeExpression
|
257 |
Return [Call](If(obj Is Nothing, New CodeDom.CodeTypeReferenceExpression(Method.DeclaringType), obj), Method.Name, Parameters)
|
258 |
End Function
|
259 |
|
260 |
|
261 |
Call object by method name
|
262 |
|
263 |
<param name="obj"></param>
|
264 |
<param name="Name"></param>
|
265 |
<param name="Parameters"></param>
|
266 |
|
267 |
Public Function [Call](obj As CodeDom.CodeExpression, Name As String, Parameters As CodeDom.CodeExpression()) As CodeDom.CodeMethodInvokeExpression
|
268 |
Dim MethodRef As New CodeDom.CodeMethodReferenceExpression(obj, Name)
|
269 |
Dim Expression As New CodeDom.CodeMethodInvokeExpression(MethodRef, Parameters)
|
270 |
Return Expression
|
271 |
End Function
|
272 |
|
273 |
Public Function [Call](type As Type, Name As String, Parameters As CodeDom.CodeExpression()) As CodeDom.CodeMethodInvokeExpression
|
274 |
Return [Call](New CodeDom.CodeTypeReferenceExpression(type), Name, Parameters)
|
275 |
End Function
|
276 |
|
277 |
|
278 |
Call a statics function from a specific type with a known function name
|
279 |
|
280 |
<param name="type"></param>
|
281 |
<param name="Name"></param>
|
282 |
<param name="parametersValue"></param>
|
283 |
|
284 |
Public Function [Call](type As Type, Name As String, parametersValue As Object()) As CodeDom.CodeMethodInvokeExpression
|
285 |
If parametersValue.IsNullOrEmpty Then
|
286 |
Return [Call](type, Name, Parameters:={})
|
287 |
Else
|
288 |
Return [Call](type, Name, (From obj In parametersValue Select New CodeDom.CodePrimitiveExpression(obj)).ToArray)
|
289 |
End If
|
290 |
End Function
|
291 |
|
292 |
|
293 |
Function returns
|
294 |
|
295 |
<param name="variable"></param>
|
296 |
|
297 |
Public Function [Return](variable As String) As CodeDom.CodeMethodReturnStatement
|
298 |
Return New CodeDom.CodeMethodReturnStatement(New CodeDom.CodeVariableReferenceExpression(variable))
|
299 |
End Function
|
300 |
|
301 |
|
302 |
Returns value in a function body
|
303 |
|
304 |
<param name="expression"></param>
|
305 |
|
306 |
Public Function [Return](expression As CodeDom.CodeExpression) As CodeDom.CodeMethodReturnStatement
|
307 |
Return New CodeDom.CodeMethodReturnStatement(expression)
|
308 |
End Function
|
309 |
|
310 |
|
311 |
Reference to a statics field in the specific target type
|
312 |
|
313 |
<param name="obj"></param>
|
314 |
<param name="Name"></param>
|
315 |
|
316 |
Public Function Reference(obj As Type, Name As String) As CodeDom.CodeFieldReferenceExpression
|
317 |
Return New CodeDom.CodeFieldReferenceExpression(New CodeDom.CodeTypeReferenceExpression(obj), Name)
|
318 |
End Function
|
319 |
|
320 |
|
321 |
Reference to a instance field in the specific object instance.
|
322 |
|
323 |
<param name="obj"></param>
|
324 |
<param name="Name"></param>
|
325 |
|
326 |
Public Function Reference(obj As CodeDom.CodeExpression, Name As String) As CodeDom.CodeFieldReferenceExpression
|
327 |
Return New CodeDom.CodeFieldReferenceExpression(obj, Name)
|
328 |
End Function
|
329 |
|
330 |
|
331 |
``left = value``
|
332 |
|
333 |
<param name="LeftAssigned"></param>
|
334 |
<param name="value"></param>
|
335 |
|
336 |
Public Function ValueAssign(LeftAssigned As CodeDom.CodeExpression, value As CodeDom.CodeExpression) As CodeDom.CodeAssignStatement
|
337 |
Return New CodeDom.CodeAssignStatement(LeftAssigned, value)
|
338 |
End Function
|
339 |
|
340 |
|
341 |
Variable value initializer
|
342 |
|
343 |
<param name="obj"></param>
|
344 |
|
345 |
Public Function Value(obj As Object) As CodeDom.CodePrimitiveExpression
|
346 |
Return New CodeDom.CodePrimitiveExpression(obj)
|
347 |
End Function
|
348 |
|
349 |
|
350 |
Reference to a local variable in a function body.(引用局部变量)
|
351 |
|
352 |
<param name="Name"></param>
|
353 |
|
354 |
Public Function LocalVariable(Name As String) As CodeDom.CodeVariableReferenceExpression
|
355 |
Return New CodeDom.CodeVariableReferenceExpression(Name)
|
356 |
End Function
|
357 |
|
358 |
|
359 |
Gets the element value in a array object.
|
360 |
|
361 |
<param name="Array"></param>
|
362 |
<param name="index"></param>
|
363 |
|
364 |
Public Function GetValue(Array As CodeDom.CodeExpression, index As Integer) As CodeDom.CodeArrayIndexerExpression
|
365 |
Dim idx = New CodeDom.CodePrimitiveExpression(index)
|
366 |
Return New CodeDom.CodeArrayIndexerExpression(Array, idx)
|
367 |
End Function
|
368 |
|
369 |
Public Function Type(Of T)() As CodeDom.CodeTypeReference
|
370 |
Dim refType As Type = GetType(T)
|
371 |
Return New CodeDom.CodeTypeReference(refType)
|
372 |
End Function
|
373 |
|
374 |
<Extension> Public Function TypeRef(type As Type) As CodeDom.CodeTypeReference
|
375 |
Return New CodeDom.CodeTypeReference(type)
|
376 |
End Function
|
377 |
|
378 |
|
379 |
System.Type.GetType(TypeName)
|
380 |
|
381 |
<param name="Type"></param>
|
382 |
|
383 |
Public Function [GetType](Type As Type) As CodeDom.CodeMethodInvokeExpression
|
384 |
Return [Call](GetType(System.Type), NameOf(System.Type.GetType), parametersValue:={Type.FullName, True, False})
|
385 |
End Function
|
386 |
|
387 |
Public Function Argument(Name As String, Type As Type) As CodeDom.CodeParameterDeclarationExpression
|
388 |
Return New CodeDom.CodeParameterDeclarationExpression(New CodeDom.CodeTypeReference(Type), Name)
|
389 |
End Function
|
390 |
|
391 |
Public Function Argument(Of T)(Name As String) As CodeDom.CodeParameterDeclarationExpression
|
392 |
Return Argument(Name, GetType(T))
|
393 |
End Function
|
394 |
|
395 |
End Module
|
396 |
End Namespace
|