-
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathExpressionPromoter.cs
More file actions
140 lines (125 loc) · 4.91 KB
/
ExpressionPromoter.cs
File metadata and controls
140 lines (125 loc) · 4.91 KB
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
using System.Linq.Expressions;
using System.Reflection;
namespace System.Linq.Dynamic.Core.Parser;
/// <summary>
/// ExpressionPromoter
/// </summary>
public class ExpressionPromoter : IExpressionPromoter
{
private readonly NumberParser _numberParser;
private readonly ConstantExpressionHelper _constantExpressionHelper;
/// <summary>
/// Initializes a new instance of the <see cref="ExpressionPromoter"/> class.
/// </summary>
/// <param name="config">The ParsingConfig.</param>
public ExpressionPromoter(ParsingConfig config)
{
_numberParser = new NumberParser(config);
_constantExpressionHelper = ConstantExpressionHelperFactory.GetInstance(config);
}
/// <inheritdoc />
public virtual Expression? Promote(Expression sourceExpression, Type type, bool exact, bool convertExpression)
{
Type returnType;
if (sourceExpression is LambdaExpression lambdaExpression)
{
returnType = lambdaExpression.GetReturnType();
}
else
{
returnType = sourceExpression.Type;
}
if (returnType == type || type.IsGenericParameter)
{
return sourceExpression;
}
if (sourceExpression is ConstantExpression ce)
{
if (Constants.IsNull(ce))
{
if (!type.GetTypeInfo().IsValueType || TypeHelper.IsNullableType(type))
{
return Expression.Constant(null, type);
}
}
else
{
if (!_constantExpressionHelper.TryGetText(ce, out var text))
{
text = ce.Value?.ToString();
}
if (text != null)
{
Type target = TypeHelper.GetNonNullableType(type);
object? value = null;
#if !(UAP10_0 || NETSTANDARD)
switch (Type.GetTypeCode(ce.Type))
{
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
value = _numberParser.ParseNumber(text, target);
// Make sure an enum value stays an enum value
if (target.IsEnum)
{
TypeHelper.TryParseEnum(text, target, out value);
}
break;
case TypeCode.Double:
if (target == typeof(decimal) || target == typeof(double))
{
value = _numberParser.ParseNumber(text, target);
}
break;
case TypeCode.String:
TypeHelper.TryParseEnum(text, target, out value);
break;
}
#else
if (ce.Type == typeof(int) || ce.Type == typeof(uint) || ce.Type == typeof(long) || ce.Type == typeof(ulong))
{
// If target is an enum value, just use the Value from the ConstantExpression
if (target.GetTypeInfo().IsEnum)
{
value = Enum.ToObject(target, ce.Value);
}
else
{
value = _numberParser.ParseNumber(text!, target);
}
}
else if (ce.Type == typeof(double))
{
if (target == typeof(decimal) || target == typeof(double))
{
value = _numberParser.ParseNumber(text, target);
}
}
else if (ce.Type == typeof(string) && TypeHelper.TryParseEnum(text, target, out value))
{
// Empty if
}
#endif
if (value != null)
{
return Expression.Constant(value, type);
}
}
}
}
if (TypeHelper.IsCompatibleWith(returnType, type))
{
if (TypeHelper.TypesAreEqual(type, typeof(decimal)) && TypeHelper.IsEnumType(sourceExpression.Type))
{
return Expression.Convert(Expression.Convert(sourceExpression, Enum.GetUnderlyingType(sourceExpression.Type)), type);
}
if (type.GetTypeInfo().IsValueType || exact || sourceExpression.Type.GetTypeInfo().IsValueType && convertExpression)
{
return Expression.Convert(sourceExpression, type);
}
return sourceExpression;
}
return null;
}
}