-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionHelper.cs
195 lines (160 loc) · 7.92 KB
/
ExpressionHelper.cs
1
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace JJ.Framework.Reflection
{
public static class ExpressionHelper
{
// GetName
/// <summary>
/// Gets the member name from the expression.
/// If the expression contains more than one member,
/// the last member name is returned.
/// </summary>
public static string GetName<T>(Expression<Func<T>> expression) => GetName((LambdaExpression)expression);
/// <summary>
/// Gets the member name from the expression.
/// If the expression contains more than one member,
/// the last member name is returned.
/// </summary>
public static string GetName(Expression<Action> expression) => GetName((LambdaExpression)expression);
/// <summary>
/// Gets the member name from the expression.
/// If the expression contains more than one member,
/// the last member is returned.
/// </summary>
public static string GetName(LambdaExpression expression) => GetMember(expression).Name;
// GetMember
/// <summary>
/// Gets the MemberInfo from the expression.
/// If the expression contains more than one member access,
/// the last member is returned.
/// </summary>
public static MemberInfo GetMember<T>(Expression<Func<T>> expression) => GetMember((LambdaExpression)expression);
/// <summary>
/// Gets the MemberInfo from the expression.
/// If the expression contains more than one member access,
/// the last member is returned.
/// </summary>
public static MemberInfo GetMember(LambdaExpression expression)
{
if (expression == null) throw new ArgumentNullException(nameof(expression));
switch (expression.Body.NodeType)
{
case ExpressionType.MemberAccess:
var memberExpression = (MemberExpression)expression.Body;
return memberExpression.Member;
case ExpressionType.Call:
var methodCallExpression = (MethodCallExpression)expression.Body;
return methodCallExpression.Method;
default:
throw new NotSupportedException($"Member cannot be retrieved from NodeType {expression.Body.NodeType}.");
}
}
// GetValue
// TODO: You might be able to make a 'fast' variation for GetValue, for simple expressions.
/// <summary>
/// Gets a value from an expression.
/// Supports field access, property access, method calls with parameters,
/// indexers, array lengths, conversion expressions, params (variable amount of arguments),
/// and both static and instance member access.
/// </summary>
public static T GetValue<T>(Expression<Func<T>> expression) => (T)GetValue((LambdaExpression)expression);
/// <summary>
/// Gets a value from an expression.
/// Supports field access, property access, method calls with parameters,
/// indexers, array lengths, conversion expressions, params (variable amount of arguments),
/// and both static and instance member access.
/// </summary>
public static object GetValue(LambdaExpression expression) => GetValue(expression?.Body);
/// <summary>
/// Gets a value from an expression.
/// Supports field access, property access, method calls with parameters,
/// indexers, array lengths, conversion expressions, params (variable amount of arguments),
/// and both static and instance member access.
/// </summary>
public static object GetValue(Expression expression)
{
if (expression == null) throw new ArgumentNullException(nameof(expression));
var translator = new ExpressionToValueTranslator();
object result = translator.GetValue(expression);
return result;
}
// GetValues
public static IList<object> GetValues<T>(Expression<Func<T>> expression) => GetValues((LambdaExpression)expression);
public static IList<object> GetValues(LambdaExpression expression) => GetValues(expression?.Body);
public static IList<object> GetValues(Expression expression)
{
if (expression == null) throw new ArgumentNullException(nameof(expression));
var translator = new ExpressionToValueTranslator();
IList<object> result = translator.GetValues(expression);
return result;
}
// GetText
/// <param name="showIndexerValues">
/// If you set this to true, an expression like MyArray[i] will translate to e.g.
/// "MyArray[2]" instead of "MyArray[i]".
/// </param>
public static string GetText<T>(Expression<Func<T>> expression, bool showIndexerValues = false) => GetText((LambdaExpression)expression, showIndexerValues);
/// <param name="showIndexerValues">
/// If you set this to true, an expression like MyArray[i] will translate to e.g.
/// "MyArray[2]" instead of "MyArray[i]".
/// </param>
public static string GetText(LambdaExpression expression, bool showIndexerValues = false) => GetText(expression?.Body, showIndexerValues);
/// <param name="showIndexerValues">
/// If you set this to true, an expression like MyArray[i] will translate to e.g.
/// "MyArray[2]" instead of "MyArray[i]".
/// </param>
public static string GetText(Expression expression, bool showIndexerValues = false)
{
var translator = new ExpressionToTextTranslator
{
ShowIndexerValues = showIndexerValues
};
string result = translator.Execute(expression);
return result;
}
// GetMethodCallInfo
/// <summary>
/// For instance:
///
/// ExpressionHelper.GetMethodCallInfo(() => MyMethod(3));
///
/// Will return:
///
/// MethodCallInfo
/// {
/// Name = "MyMethod",
/// Parameters =
/// {
/// ParameterType = typeof(int),
/// Name = "myParameter",
/// Value = 3
/// }
/// };
/// </summary>
public static MethodCallInfo GetMethodCallInfo(LambdaExpression expression)
{
if (expression == null) throw new ArgumentNullException(nameof(expression));
switch (expression.Body.NodeType)
{
case ExpressionType.Call:
var methodCallExpression = (MethodCallExpression)expression.Body;
var methodCallInfo = new MethodCallInfo(methodCallExpression.Method.Name);
IList<ParameterInfo> parameters = methodCallExpression.Method.GetParameters();
for (var i = 0; i < parameters.Count; i++)
{
ParameterInfo parameter = parameters[i];
Expression argumentExpression = methodCallExpression.Arguments[i];
object value = GetValue(argumentExpression);
var methodCallParameterInfo = new MethodCallParameterInfo(parameter.ParameterType, parameter.Name, value);
methodCallInfo.Parameters.Add(methodCallParameterInfo);
}
return methodCallInfo;
default:
throw new NotSupportedException($"MethodCallInfo cannot be retrieved from NodeType {expression.Body.NodeType}.");
}
}
}
}