-
Notifications
You must be signed in to change notification settings - Fork 456
/
ExpressionExtension.cs
221 lines (186 loc) · 7.02 KB
/
ExpressionExtension.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using Chloe.Visitors;
using Chloe.Reflection;
using System.Linq.Expressions;
using System.Reflection;
namespace Chloe.Extensions
{
public static class ExpressionExtension
{
public static bool IsEvaluable(this Expression exp)
{
return ExpressionEvaluableJudgment.IsEvaluable(exp);
}
public static object Evaluate(this Expression exp)
{
return ExpressionEvaluator.Evaluate(exp);
}
public static BinaryExpression Assign(MemberInfo propertyOrField, Expression instance, Expression value)
{
var member = MakeMemberExpression(propertyOrField, instance);
var setValue = Expression.Assign(member, value);
return setValue;
}
public static MemberExpression MakeMemberExpression(MemberInfo propertyOrField, Expression instance)
{
if (propertyOrField.MemberType == MemberTypes.Property)
{
var prop = Expression.Property(instance, (PropertyInfo)propertyOrField);
return prop;
}
if (propertyOrField.MemberType == MemberTypes.Field)
{
var field = Expression.Field(instance, (FieldInfo)propertyOrField);
return field;
}
throw new ArgumentException();
}
public static LambdaExpression AndAlso(this LambdaExpression a, LambdaExpression b)
{
if (a == null)
return b;
if (b == null)
return a;
Type rootType = a.Parameters[0].Type;
var memberParam = Expression.Parameter(rootType, "root");
var aNewBody = ParameterExpressionReplacer.Replace(a.Body, memberParam);
var bNewBody = ParameterExpressionReplacer.Replace(b.Body, memberParam);
var newBody = Expression.AndAlso(aNewBody, bNewBody);
var lambda = Expression.Lambda(a.Type, newBody, memberParam);
return lambda;
}
internal static bool IsDerivedFromParameter(this MemberExpression exp)
{
ParameterExpression p;
return IsDerivedFromParameter(exp, out p);
}
internal static bool IsDerivedFromParameter(this MemberExpression exp, out ParameterExpression p)
{
p = null;
MemberExpression memberExp = exp;
Expression prevExp;
do
{
prevExp = memberExp.Expression;
memberExp = prevExp as MemberExpression;
} while (memberExp != null);
if (prevExp == null)/* 静态属性访问 */
return false;
if (prevExp.NodeType == ExpressionType.Parameter)
{
p = (ParameterExpression)prevExp;
return true;
}
/* 当实体继承于某个接口或类时,会有这种情况 */
if (prevExp.NodeType == ExpressionType.Convert)
{
prevExp = ((UnaryExpression)prevExp).Operand;
if (prevExp.NodeType == ExpressionType.Parameter)
{
p = (ParameterExpression)prevExp;
return true;
}
}
return false;
}
public static Expression StripQuotes(this Expression exp)
{
while (exp.NodeType == ExpressionType.Quote)
{
exp = ((UnaryExpression)exp).Operand;
}
return exp;
}
public static Expression StripConvert(this Expression exp)
{
Expression operand = exp;
while (operand.NodeType == ExpressionType.Convert || operand.NodeType == ExpressionType.ConvertChecked)
{
operand = ((UnaryExpression)operand).Operand;
}
return operand;
}
public static Stack<MemberExpression> Reverse(this MemberExpression exp)
{
var stack = new Stack<MemberExpression>();
do
{
stack.Push(exp);
exp = exp.Expression as MemberExpression;
} while (exp != null);
return stack;
}
public static Expression MakeWrapperAccess(object value, Type targetType)
{
if (value == null)
{
if (targetType != null)
return Expression.Constant(value, targetType);
else
return Expression.Constant(value, typeof(object));
}
object wrapper = WrapValue(value);
ConstantExpression wrapperConstantExp = Expression.Constant(wrapper);
Expression ret = Expression.MakeMemberAccess(wrapperConstantExp, wrapper.GetType().GetProperty("Value"));
if (ret.Type != targetType)
{
ret = Expression.Convert(ret, targetType);
}
return ret;
}
static object WrapValue(object value)
{
if (value == null)
{
return null;
}
Type valueType = value.GetType();
if (valueType == PublicConstants.TypeOfString)
{
return new ConstantWrapper<string>((string)value);
}
if (valueType == PublicConstants.TypeOfInt32)
{
return new ConstantWrapper<int>((int)value);
}
if (valueType == PublicConstants.TypeOfInt64)
{
return new ConstantWrapper<long>((long)value);
}
if (valueType == PublicConstants.TypeOfByte)
{
return new ConstantWrapper<byte>((byte)value);
}
if (valueType == PublicConstants.TypeOfSingle)
{
return new ConstantWrapper<float>((float)value);
}
if (valueType == PublicConstants.TypeOfDouble)
{
return new ConstantWrapper<double>((double)value);
}
if (valueType == PublicConstants.TypeOfDecimal)
{
return new ConstantWrapper<decimal>((decimal)value);
}
if (valueType == PublicConstants.TypeOfGuid)
{
return new ConstantWrapper<Guid>((Guid)value);
}
if (valueType == PublicConstants.TypeOfDateTime)
{
return new ConstantWrapper<DateTime>((DateTime)value);
}
if (valueType == PublicConstants.TypeOfBoolean)
{
return new ConstantWrapper<bool>((bool)value);
}
if (valueType == PublicConstants.TypeOfChar)
{
return new ConstantWrapper<char>((char)value);
}
Type wrapperType = typeof(ConstantWrapper<>).MakeGenericType(valueType);
ConstructorInfo constructor = wrapperType.GetConstructor(new Type[] { valueType });
return constructor.FastCreateInstance(new object[] { value });
}
}
}