1212using Xtensive . Core ;
1313using Xtensive . Linq ;
1414using Xtensive . Orm . Internals ;
15- using Xtensive . Orm . Rse ;
1615using Xtensive . Reflection ;
1716using ExpressionVisitor = Xtensive . Linq . ExpressionVisitor ;
1817
@@ -34,24 +33,27 @@ internal sealed class ExpressionEvaluator : ExpressionVisitor
3433 /// <returns>
3534 /// <see langword="true" /> if <paramref name="e"/> can be evaluated; otherwise, <see langword="false" />.
3635 /// </returns>
37- public bool CanBeEvaluated ( Expression e )
38- {
39- return candidates . Contains ( e ) ;
40- }
36+ public bool CanBeEvaluated ( Expression e ) => candidates . Contains ( e ) ;
4137
4238 /// <summary>
4339 /// Evaluates the specified <paramref name="e"/> into <see cref="ConstantExpression"/>.
4440 /// </summary>
4541 /// <param name="e">The expression.</param>
4642 public static ConstantExpression Evaluate ( Expression e )
4743 {
48- if ( e == null )
44+ if ( e == null ) {
4945 return null ;
50- if ( e . NodeType == ExpressionType . Constant )
46+ }
47+
48+ if ( e . NodeType == ExpressionType . Constant ) {
5149 return ( ConstantExpression ) e ;
52- Type type = e . Type ;
53- if ( type . IsValueType )
50+ }
51+
52+ var type = e . Type ;
53+ if ( type . IsValueType ) {
5454 e = Expression . Convert ( e , WellKnownTypes . Object ) ;
55+ }
56+
5557 var lambda = FastExpression . Lambda < Func < object > > ( e ) ;
5658 var func = lambda . CachingCompile ( ) ;
5759 return Expression . Constant ( func ( ) , type ) ;
@@ -60,63 +62,73 @@ public static ConstantExpression Evaluate(Expression e)
6062 /// <inheritdoc/>
6163 protected override Expression Visit ( Expression e )
6264 {
63- if ( e != null ) {
64- bool saved = couldBeEvaluated ;
65+ if ( e != null ) {
66+ var saved = couldBeEvaluated ;
6567 couldBeEvaluated = true ;
6668 base . Visit ( e ) ;
67- if ( couldBeEvaluated )
68- if ( CanEvaluateExpression ( e ) )
69+ if ( couldBeEvaluated ) {
70+ if ( CanEvaluateExpression ( e ) ) {
6971 candidates . Add ( e ) ;
70- else
72+ }
73+ else {
7174 couldBeEvaluated = false ;
75+ }
76+ }
77+
7278 couldBeEvaluated &= saved ;
7379 }
80+
7481 return e ;
7582 }
7683
7784 /// <inheritdoc/>
78- protected override Expression VisitUnknown ( Expression e )
79- {
80- return e ;
81- }
85+ protected override Expression VisitUnknown ( Expression e ) => e ;
8286
8387 // Private methods
8488
8589 private static bool CanEvaluateExpression ( Expression expression )
8690 {
87- if ( expression . Type == WellKnownOrmTypes . ApplyParameter )
91+ if ( expression . Type == WellKnownOrmTypes . ApplyParameter ) {
8892 return false ;
89- var cex = expression as ConstantExpression ;
90- if ( cex != null ) {
91- var query = cex . Value as IQueryable ;
92- return query == null ;
9393 }
94- if ( expression . NodeType == ExpressionType . MemberAccess ) {
94+
95+ if ( expression is ConstantExpression cex ) {
96+ return ! ( cex . Value is IQueryable ) ;
97+ }
98+
99+ if ( expression . NodeType == ExpressionType . MemberAccess ) {
95100 var ma = ( MemberExpression ) expression ;
96- if ( ma . Expression == null )
101+ if ( ma . Expression == null ) {
97102 return ! WellKnownInterfaces . Queryable . IsAssignableFrom ( ma . Type ) ;
98- if ( ma . Expression . Type . IsNullable ( ) && ma . Member . Name == "Value" )
103+ }
104+
105+ if ( ma . Expression . Type . IsNullable ( ) && ma . Member . Name == "Value" ) {
99106 return false ;
100- if ( ma . Expression . NodeType == ExpressionType . Constant ) {
107+ }
108+
109+ if ( ma . Expression . NodeType == ExpressionType . Constant ) {
101110 var rfi = ma . Member as FieldInfo ;
102- if ( rfi != null && rfi . FieldType . IsGenericType && WellKnownInterfaces . Queryable . IsAssignableFrom ( rfi . FieldType ) )
111+ if ( rfi != null && rfi . FieldType . IsGenericType
112+ && WellKnownInterfaces . Queryable . IsAssignableFrom ( rfi . FieldType ) ) {
103113 return false ;
114+ }
104115 }
105116 }
106- var mc = expression as MethodCallExpression ;
107- #pragma warning disable 612 , 618
108- if ( mc != null ) {
117+
118+ if ( expression is MethodCallExpression mc ) {
109119 var methodInfo = mc . Method ;
110120 if ( methodInfo . DeclaringType == WellKnownTypes . Enumerable ||
111- methodInfo . DeclaringType == WellKnownTypes . Queryable ||
112- ( methodInfo . DeclaringType == WellKnownOrmTypes . Query ) && methodInfo . IsGenericMethod )
121+ methodInfo . DeclaringType == WellKnownTypes . Queryable ||
122+ methodInfo . DeclaringType == WellKnownOrmTypes . Query && methodInfo . IsGenericMethod ) {
113123 return false ;
124+ }
114125 }
115- #pragma warning restore 612 , 618
116- if ( expression . NodeType == ExpressionType . Convert && expression . Type == WellKnownTypes . Object )
126+
127+ if ( expression . NodeType == ExpressionType . Convert && expression . Type == WellKnownTypes . Object ) {
117128 return true ;
118- return expression . NodeType != ExpressionType . Parameter &&
119- expression . NodeType != ExpressionType . Lambda ;
129+ }
130+
131+ return expression . NodeType != ExpressionType . Parameter && expression . NodeType != ExpressionType . Lambda ;
120132 }
121133
122134
0 commit comments