@@ -24,6 +24,7 @@ public class ExpressionParser
24
24
static readonly string methodThenByDescending = nameof ( Queryable . ThenByDescending ) ;
25
25
26
26
private readonly ParsingConfig _parsingConfig ;
27
+ private readonly MethodFinder _methodFinder ;
27
28
private readonly KeywordsHelper _keywordsHelper ;
28
29
private readonly TextParser _textParser ;
29
30
private readonly Dictionary < string , object > _internals ;
@@ -64,6 +65,7 @@ public ExpressionParser([CanBeNull] ParameterExpression[] parameters, [NotNull]
64
65
65
66
_keywordsHelper = new KeywordsHelper ( _parsingConfig ) ;
66
67
_textParser = new TextParser ( expression ) ;
68
+ _methodFinder = new MethodFinder ( _parsingConfig ) ;
67
69
}
68
70
69
71
void ProcessParameters ( ParameterExpression [ ] parameters )
@@ -130,7 +132,7 @@ public Expression Parse([CanBeNull] Type resultType, bool createParameterCtor =
130
132
131
133
if ( resultType != null )
132
134
{
133
- if ( ( expr = ExpressionPromoter . Promote ( expr , resultType , true , false ) ) == null )
135
+ if ( ( expr = _parsingConfig . ExpressionPromoter . Promote ( expr , resultType , true , false ) ) == null )
134
136
{
135
137
throw ParseError ( exprPos , Res . ExpressionTypeMismatch , TypeHelper . GetTypeName ( resultType ) ) ;
136
138
}
@@ -352,7 +354,7 @@ Expression ParseIn()
352
354
353
355
var args = new [ ] { left } ;
354
356
355
- if ( MethodFinder . FindMethod ( typeof ( IEnumerableSignatures ) , nameof ( IEnumerableSignatures . Contains ) , false , args , out MethodBase containsSignature ) != 1 )
357
+ if ( _methodFinder . FindMethod ( typeof ( IEnumerableSignatures ) , nameof ( IEnumerableSignatures . Contains ) , false , args , out MethodBase containsSignature ) != 1 )
356
358
{
357
359
throw ParseError ( op . Pos , Res . NoApplicableAggregate , nameof ( IEnumerableSignatures . Contains ) ) ;
358
360
}
@@ -469,11 +471,11 @@ Expression ParseComparisonOperator()
469
471
if ( left . Type != right . Type )
470
472
{
471
473
Expression e ;
472
- if ( ( e = ExpressionPromoter . Promote ( right , left . Type , true , false ) ) != null )
474
+ if ( ( e = _parsingConfig . ExpressionPromoter . Promote ( right , left . Type , true , false ) ) != null )
473
475
{
474
476
right = e ;
475
477
}
476
- else if ( ( e = ExpressionPromoter . Promote ( left , right . Type , true , false ) ) != null )
478
+ else if ( ( e = _parsingConfig . ExpressionPromoter . Promote ( left , right . Type , true , false ) ) != null )
477
479
{
478
480
left = e ;
479
481
}
@@ -1059,8 +1061,8 @@ Expression GenerateConditional(Expression test, Expression expr1, Expression exp
1059
1061
1060
1062
if ( expr1 . Type != expr2 . Type )
1061
1063
{
1062
- Expression expr1As2 = expr2 != Constants . NullLiteral ? ExpressionPromoter . Promote ( expr1 , expr2 . Type , true , false ) : null ;
1063
- Expression expr2As1 = expr1 != Constants . NullLiteral ? ExpressionPromoter . Promote ( expr2 , expr1 . Type , true , false ) : null ;
1064
+ Expression expr1As2 = expr2 != Constants . NullLiteral ? _parsingConfig . ExpressionPromoter . Promote ( expr1 , expr2 . Type , true , false ) : null ;
1065
+ Expression expr2As1 = expr1 != Constants . NullLiteral ? _parsingConfig . ExpressionPromoter . Promote ( expr2 , expr1 . Type , true , false ) : null ;
1064
1066
if ( expr1As2 != null && expr2As1 == null )
1065
1067
{
1066
1068
expr1 = expr1As2 ;
@@ -1200,7 +1202,7 @@ private Expression CreateArrayInitializerExpression(List<Expression> expressions
1200
1202
1201
1203
if ( newType != null )
1202
1204
{
1203
- return Expression . NewArrayInit ( newType , expressions . Select ( expression => ExpressionPromoter . Promote ( expression , newType , true , true ) ) ) ;
1205
+ return Expression . NewArrayInit ( newType , expressions . Select ( expression => _parsingConfig . ExpressionPromoter . Promote ( expression , newType , true , true ) ) ) ;
1204
1206
}
1205
1207
1206
1208
return Expression . NewArrayInit ( expressions . All ( expression => expression . Type == expressions [ 0 ] . Type ) ? expressions [ 0 ] . Type : typeof ( object ) , expressions ) ;
@@ -1270,7 +1272,7 @@ private Expression CreateNewExpression(List<DynamicProperty> properties, List<Ex
1270
1272
Type expressionType = expressions [ i ] . Type ;
1271
1273
1272
1274
// Promote from Type to Nullable Type if needed
1273
- expressionsPromoted . Add ( ExpressionPromoter . Promote ( expressions [ i ] , propertyType , true , true ) ) ;
1275
+ expressionsPromoted . Add ( _parsingConfig . ExpressionPromoter . Promote ( expressions [ i ] , propertyType , true , true ) ) ;
1274
1276
}
1275
1277
1276
1278
return Expression . New ( ctor , expressionsPromoted , ( IEnumerable < MemberInfo > ) propertyInfos ) ;
@@ -1284,7 +1286,7 @@ private Expression CreateNewExpression(List<DynamicProperty> properties, List<Ex
1284
1286
Type expressionType = expressions [ i ] . Type ;
1285
1287
1286
1288
// Promote from Type to Nullable Type if needed
1287
- bindings [ i ] = Expression . Bind ( property , ExpressionPromoter . Promote ( expressions [ i ] , propertyType , true , true ) ) ;
1289
+ bindings [ i ] = Expression . Bind ( property , _parsingConfig . ExpressionPromoter . Promote ( expressions [ i ] , propertyType , true , true ) ) ;
1288
1290
}
1289
1291
1290
1292
return Expression . MemberInit ( Expression . New ( type ) , bindings ) ;
@@ -1295,7 +1297,7 @@ Expression ParseLambdaInvocation(LambdaExpression lambda)
1295
1297
int errorPos = _textParser . CurrentToken . Pos ;
1296
1298
_textParser . NextToken ( ) ;
1297
1299
Expression [ ] args = ParseArgumentList ( ) ;
1298
- if ( MethodFinder . FindMethod ( lambda . Type , nameof ( Expression . Invoke ) , false , args , out MethodBase _ ) != 1 )
1300
+ if ( _methodFinder . FindMethod ( lambda . Type , nameof ( Expression . Invoke ) , false , args , out MethodBase _ ) != 1 )
1299
1301
{
1300
1302
throw ParseError ( errorPos , Res . ArgsIncompatibleWithLambda ) ;
1301
1303
}
@@ -1336,7 +1338,7 @@ Expression ParseTypeAccess(Type type)
1336
1338
}
1337
1339
}
1338
1340
1339
- switch ( MethodFinder . FindBestMethod ( type . GetConstructors ( ) , args , out MethodBase method ) )
1341
+ switch ( _methodFinder . FindBestMethod ( type . GetConstructors ( ) , args , out MethodBase method ) )
1340
1342
{
1341
1343
case 0 :
1342
1344
if ( args . Length == 1 )
@@ -1443,7 +1445,7 @@ Expression ParseMemberAccess(Type type, Expression instance)
1443
1445
}
1444
1446
1445
1447
Expression [ ] args = ParseArgumentList ( ) ;
1446
- switch ( MethodFinder . FindMethod ( type , id , instance == null , args , out MethodBase mb ) )
1448
+ switch ( _methodFinder . FindMethod ( type , id , instance == null , args , out MethodBase mb ) )
1447
1449
{
1448
1450
case 0 :
1449
1451
throw ParseError ( errorPos , Res . NoApplicableMethod , id , TypeHelper . GetTypeName ( type ) ) ;
@@ -1587,13 +1589,13 @@ Expression ParseAggregate(Expression instance, Type elementType, string methodNa
1587
1589
_it = outerIt ;
1588
1590
_parent = oldParent ;
1589
1591
1590
- if ( ! MethodFinder . ContainsMethod ( typeof ( IEnumerableSignatures ) , methodName , false , args ) )
1592
+ if ( ! _methodFinder . ContainsMethod ( typeof ( IEnumerableSignatures ) , methodName , false , args ) )
1591
1593
{
1592
1594
throw ParseError ( errorPos , Res . NoApplicableAggregate , methodName ) ;
1593
1595
}
1594
1596
1595
1597
Type callType = typeof ( Enumerable ) ;
1596
- if ( isQueryable && MethodFinder . ContainsMethod ( typeof ( IQueryableSignatures ) , methodName , false , args ) )
1598
+ if ( isQueryable && _methodFinder . ContainsMethod ( typeof ( IQueryableSignatures ) , methodName , false , args ) )
1597
1599
{
1598
1600
callType = typeof ( Queryable ) ;
1599
1601
}
@@ -1693,7 +1695,7 @@ Expression ParseElementAccess(Expression expr)
1693
1695
{
1694
1696
throw ParseError ( errorPos , Res . CannotIndexMultiDimArray ) ;
1695
1697
}
1696
- Expression index = ExpressionPromoter . Promote ( args [ 0 ] , typeof ( int ) , true , false ) ;
1698
+ Expression index = _parsingConfig . ExpressionPromoter . Promote ( args [ 0 ] , typeof ( int ) , true , false ) ;
1697
1699
1698
1700
if ( index == null )
1699
1701
{
@@ -1703,7 +1705,7 @@ Expression ParseElementAccess(Expression expr)
1703
1705
return Expression . ArrayIndex ( expr , index ) ;
1704
1706
}
1705
1707
1706
- switch ( MethodFinder . FindIndexer ( expr . Type , args , out var mb ) )
1708
+ switch ( _methodFinder . FindIndexer ( expr . Type , args , out var mb ) )
1707
1709
{
1708
1710
case 0 :
1709
1711
throw ParseError ( errorPos , Res . NoApplicableIndexer ,
@@ -1758,7 +1760,7 @@ void CheckAndPromoteOperand(Type signatures, string opName, ref Expression expr,
1758
1760
{
1759
1761
Expression [ ] args = { expr } ;
1760
1762
1761
- if ( ! MethodFinder . ContainsMethod ( signatures , "F" , false , args ) )
1763
+ if ( ! _methodFinder . ContainsMethod ( signatures , "F" , false , args ) )
1762
1764
{
1763
1765
throw IncompatibleOperandError ( opName , expr , errorPos ) ;
1764
1766
}
@@ -1770,7 +1772,7 @@ void CheckAndPromoteOperands(Type signatures, string opName, ref Expression left
1770
1772
{
1771
1773
Expression [ ] args = { left , right } ;
1772
1774
1773
- if ( ! MethodFinder . ContainsMethod ( signatures , "F" , false , args ) )
1775
+ if ( ! _methodFinder . ContainsMethod ( signatures , "F" , false , args ) )
1774
1776
{
1775
1777
throw IncompatibleOperandsError ( opName , left , right , errorPos ) ;
1776
1778
}
0 commit comments