1 |
#Region "Microsoft.VisualBasic::3fabc6b0b3cb9576aed216e7ae407c6c, Microsoft.VisualBasic.Core\Extensions\Reflection\Parameters\ParamLogUtility.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 |
#End Region
|
44 |
|
45 |
Imports System.Linq.Expressions
|
46 |
Imports System.Reflection
|
47 |
Imports System.Runtime.CompilerServices
|
48 |
Imports Microsoft.VisualBasic.ComponentModel.Collection
|
49 |
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
|
50 |
Imports Microsoft.VisualBasic.Language
|
51 |
|
52 |
Namespace Emit.Parameters
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
Public Module ParamLogUtility
|
64 |
|
65 |
Public Function GetMyCaller() As MethodBase
|
66 |
|
67 |
|
68 |
|
69 |
Return New StackTrace().GetFrame(2).GetMethod()
|
70 |
End Function
|
71 |
|
72 |
Public Function Acquire(ParamArray providedParameters As Expression(Of Func(Of Object))()) As Dictionary(Of Value)
|
73 |
Dim currentMethod As MethodBase = New StackTrace().GetFrame(1).GetMethod()
|
74 |
Return currentMethod.Acquire(providedParameters)
|
75 |
End Function
|
76 |
|
77 |
Public Function Acquire(Of T)(ParamArray providedParameters As Expression(Of Func(Of Object))()) As Dictionary(Of Value)
|
78 |
Dim type As Type = GetType(T)
|
79 |
Dim out As Dictionary(Of Value) = ParamLogUtility.Acquire(providedParameters)
|
80 |
out -= out.Keys.Where(Function(k) Not out(k).Type.Equals(type))
|
81 |
Return out
|
82 |
End Function
|
83 |
|
84 |
<Extension>
|
85 |
Public Function Acquire(currentMethod As MethodBase, ParamArray providedParameters As Expression(Of Func(Of Object))()) As Dictionary(Of Value)
|
86 |
Dim out As New Dictionary(Of Value)
|
87 |
|
88 |
Dim trace As New NamedValue(Of MethodBase) With {
|
89 |
.Name = currentMethod.Name,
|
90 |
.Value = currentMethod
|
91 |
}
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
For Each aExpression In providedParameters
|
105 |
Dim bodyType As Expression = aExpression.Body
|
106 |
|
107 |
If TypeOf bodyType Is MemberExpression Then
|
108 |
Call out.AddProvidedParamaterDetail(DirectCast(aExpression.Body, MemberExpression), trace)
|
109 |
ElseIf TypeOf bodyType Is UnaryExpression Then
|
110 |
Dim unaryExpression As UnaryExpression = DirectCast(aExpression.Body, UnaryExpression)
|
111 |
Call out.AddProvidedParamaterDetail(DirectCast(unaryExpression.Operand, MemberExpression), trace)
|
112 |
Else
|
113 |
Throw New Exception("Expression type unknown.")
|
114 |
End If
|
115 |
Next
|
116 |
|
117 |
Return out
|
118 |
End Function
|
119 |
|
120 |
<Extension>
|
121 |
Private Sub AddProvidedParamaterDetail(out As Dictionary(Of Value), memberExpression As MemberExpression, trace As NamedValue(Of MethodBase))
|
122 |
Dim constantExpression As ConstantExpression = DirectCast(memberExpression.Expression, ConstantExpression)
|
123 |
Dim name As String = memberExpression.Member.Name
|
124 |
Dim value = DirectCast(memberExpression.Member, FieldInfo).GetValue(constantExpression.Value)
|
125 |
Dim type As Type = value.[GetType]()
|
126 |
|
127 |
name = name.Replace("$VB$Local_", "")
|
128 |
out += New Value With {
|
129 |
.Name = name,
|
130 |
.Type = type,
|
131 |
.Value = value,
|
132 |
.Trace = trace
|
133 |
}
|
134 |
End Sub
|
135 |
|
136 |
<Extension>
|
137 |
Public Function InitTable(caller As MethodBase, array As Expression(Of Func(Of Object()))) As Dictionary(Of Value)
|
138 |
Dim unaryExpression As NewArrayExpression = DirectCast(array.Body, NewArrayExpression)
|
139 |
Dim arrayData As UnaryExpression() = unaryExpression _
|
140 |
.Expressions _
|
141 |
.Select(Function(e) DirectCast(e, UnaryExpression)) _
|
142 |
.ToArray
|
143 |
Dim out As New Dictionary(Of Value)
|
144 |
Dim trace As New NamedValue(Of MethodBase) With {
|
145 |
.Name = caller.Name,
|
146 |
.Value = caller
|
147 |
}
|
148 |
|
149 |
For Each expr As UnaryExpression In arrayData
|
150 |
Dim member = DirectCast(expr.Operand, MemberExpression)
|
151 |
Dim constantExpression As ConstantExpression = DirectCast(member.Expression, ConstantExpression)
|
152 |
Dim name As String = member.Member.Name.Replace("$VB$Local_", "")
|
153 |
Dim field As FieldInfo = DirectCast(member.Member, FieldInfo)
|
154 |
Dim value As Object = field.GetValue(constantExpression.Value)
|
155 |
|
156 |
out += New Value With {
|
157 |
.Name = name,
|
158 |
.Type = value.GetType,
|
159 |
.Value = value,
|
160 |
.Trace = trace
|
161 |
}
|
162 |
Next
|
163 |
|
164 |
Return out
|
165 |
End Function
|
166 |
End Module
|
167 |
End Namespace
|