@@ -313,9 +313,16 @@ private Expression ParseIn()
313
313
Expression right = ParseUnary ( ) ;
314
314
315
315
// if the identifier is an Enum, try to convert the right-side also to an Enum.
316
- if ( left . Type . GetTypeInfo ( ) . IsEnum && right is ConstantExpression constantExpression )
316
+ if ( left . Type . GetTypeInfo ( ) . IsEnum )
317
317
{
318
- right = ParseEnumToConstantExpression ( op . Pos , left . Type , constantExpression ) ;
318
+ if ( right is ConstantExpression constantExprRight )
319
+ {
320
+ right = ParseEnumToConstantExpression ( op . Pos , left . Type , constantExprRight ) ;
321
+ }
322
+ else if ( _expressionHelper . TryUnwrapAsConstantExpression ( right , out var unwrappedConstantExprRight ) )
323
+ {
324
+ right = ParseEnumToConstantExpression ( op . Pos , left . Type , unwrappedConstantExprRight ) ;
325
+ }
319
326
}
320
327
321
328
// else, check for direct type match
@@ -476,13 +483,27 @@ private Expression ParseComparisonOperator()
476
483
{
477
484
left = e ;
478
485
}
479
- else if ( TypeHelper . IsEnumType ( left . Type ) && ( constantExpr = right as ConstantExpression ) != null )
486
+ else if ( TypeHelper . IsEnumType ( left . Type ) )
480
487
{
481
- right = ParseEnumToConstantExpression ( op . Pos , left . Type , constantExpr ) ;
488
+ if ( right is ConstantExpression constantExprRight )
489
+ {
490
+ right = ParseEnumToConstantExpression ( op . Pos , left . Type , constantExprRight ) ;
491
+ }
492
+ else if ( _expressionHelper . TryUnwrapAsConstantExpression ( right , out var unwrappedConstantExprRight ) )
493
+ {
494
+ right = ParseEnumToConstantExpression ( op . Pos , left . Type , unwrappedConstantExprRight ) ;
495
+ }
482
496
}
483
- else if ( TypeHelper . IsEnumType ( right . Type ) && ( constantExpr = left as ConstantExpression ) != null )
497
+ else if ( TypeHelper . IsEnumType ( right . Type ) )
484
498
{
485
- left = ParseEnumToConstantExpression ( op . Pos , right . Type , constantExpr ) ;
499
+ if ( left is ConstantExpression constantExprLeft )
500
+ {
501
+ left = ParseEnumToConstantExpression ( op . Pos , right . Type , constantExprLeft ) ;
502
+ }
503
+ else if ( _expressionHelper . TryUnwrapAsConstantExpression ( left , out var unwrappedConstantExprLeft ) )
504
+ {
505
+ left = ParseEnumToConstantExpression ( op . Pos , right . Type , unwrappedConstantExprLeft ) ;
506
+ }
486
507
}
487
508
else
488
509
{
@@ -498,11 +519,11 @@ private Expression ParseComparisonOperator()
498
519
{
499
520
left = Expression . Constant ( typeConverter . ConvertFromInvariantString ( stringValueL ) , right . Type ) ;
500
521
}
501
- else if ( _expressionHelper . TryUnwrapConstantExpression < string > ( right , out var unwrappedStringValueR ) && ( typeConverter = _typeConverterFactory . GetConverter ( left . Type ) ) != null && typeConverter . CanConvertFrom ( right . Type ) )
522
+ else if ( _expressionHelper . TryUnwrapAsValue < string > ( right , out var unwrappedStringValueR ) && ( typeConverter = _typeConverterFactory . GetConverter ( left . Type ) ) != null && typeConverter . CanConvertFrom ( right . Type ) )
502
523
{
503
524
right = Expression . Constant ( typeConverter . ConvertFromInvariantString ( unwrappedStringValueR ) , left . Type ) ;
504
525
}
505
- else if ( _expressionHelper . TryUnwrapConstantExpression < string > ( left , out var unwrappedStringValueL ) && ( typeConverter = _typeConverterFactory . GetConverter ( right . Type ) ) != null && typeConverter . CanConvertFrom ( left . Type ) )
526
+ else if ( _expressionHelper . TryUnwrapAsValue < string > ( left , out var unwrappedStringValueL ) && ( typeConverter = _typeConverterFactory . GetConverter ( right . Type ) ) != null && typeConverter . CanConvertFrom ( left . Type ) )
506
527
{
507
528
left = Expression . Constant ( typeConverter . ConvertFromInvariantString ( unwrappedStringValueL ) , right . Type ) ;
508
529
}
@@ -581,7 +602,7 @@ private Expression ParseComparisonOperator()
581
602
return left ;
582
603
}
583
604
584
- private bool HasImplicitConversion ( Type baseType , Type targetType )
605
+ private static bool HasImplicitConversion ( Type baseType , Type targetType )
585
606
{
586
607
var baseTypeHasConversion = baseType . GetMethods ( BindingFlags . Public | BindingFlags . Static )
587
608
. Where ( mi => mi . Name == "op_Implicit" && mi . ReturnType == targetType )
@@ -597,12 +618,12 @@ private bool HasImplicitConversion(Type baseType, Type targetType)
597
618
. Any ( mi => mi . GetParameters ( ) . FirstOrDefault ( ) ? . ParameterType == baseType ) ;
598
619
}
599
620
600
- private ConstantExpression ParseEnumToConstantExpression ( int pos , Type leftType , ConstantExpression constantExpr )
621
+ private static ConstantExpression ParseEnumToConstantExpression ( int pos , Type leftType , ConstantExpression constantExpr )
601
622
{
602
623
return Expression . Constant ( ParseConstantExpressionToEnum ( pos , leftType , constantExpr ) , leftType ) ;
603
624
}
604
625
605
- private object ParseConstantExpressionToEnum ( int pos , Type leftType , ConstantExpression constantExpr )
626
+ private static object ParseConstantExpressionToEnum ( int pos , Type leftType , ConstantExpression constantExpr )
606
627
{
607
628
try
608
629
{
@@ -618,7 +639,7 @@ private object ParseConstantExpressionToEnum(int pos, Type leftType, ConstantExp
618
639
619
640
try
620
641
{
621
- return Enum . ToObject ( TypeHelper . GetNonNullableType ( leftType ) , constantExpr . Value ) ;
642
+ return Enum . ToObject ( TypeHelper . GetNonNullableType ( leftType ) , constantExpr . Value ! ) ;
622
643
}
623
644
catch
624
645
{
0 commit comments