7
7
using System . Linq . Dynamic . Core . Parser . SupportedMethods ;
8
8
using System . Linq . Dynamic . Core . Parser . SupportedOperands ;
9
9
using System . Linq . Dynamic . Core . Tokenizer ;
10
+ using System . Linq . Dynamic . Core . TypeConverters ;
10
11
using System . Linq . Dynamic . Core . Validation ;
11
12
using System . Linq . Expressions ;
12
13
using System . Reflection ;
@@ -29,6 +30,7 @@ public class ExpressionParser
29
30
private readonly TextParser _textParser ;
30
31
private readonly IExpressionHelper _expressionHelper ;
31
32
private readonly ITypeFinder _typeFinder ;
33
+ private readonly ITypeConverterFactory _typeConverterFactory ;
32
34
private readonly Dictionary < string , object > _internals ;
33
35
private readonly Dictionary < string , object > _symbols ;
34
36
@@ -75,6 +77,7 @@ public ExpressionParser([CanBeNull] ParameterExpression[] parameters, [NotNull]
75
77
_methodFinder = new MethodFinder ( _parsingConfig ) ;
76
78
_expressionHelper = new ExpressionHelper ( _parsingConfig ) ;
77
79
_typeFinder = new TypeFinder ( _parsingConfig , _keywordsHelper ) ;
80
+ _typeConverterFactory = new TypeConverterFactory ( _parsingConfig ) ;
78
81
}
79
82
80
83
void ProcessParameters ( ParameterExpression [ ] parameters )
@@ -477,13 +480,13 @@ Expression ParseComparisonOperator()
477
480
}
478
481
}
479
482
}
480
- else if ( ( constantExpr = right as ConstantExpression ) != null && constantExpr . Value is string && ( typeConverter = TypeConverterFactory . GetConverter ( left . Type ) ) != null )
483
+ else if ( ( constantExpr = right as ConstantExpression ) != null && constantExpr . Value is string stringValueR && ( typeConverter = _typeConverterFactory . GetConverter ( left . Type ) ) != null )
481
484
{
482
- right = Expression . Constant ( typeConverter . ConvertFromInvariantString ( ( string ) constantExpr . Value ) , left . Type ) ;
485
+ right = Expression . Constant ( typeConverter . ConvertFromInvariantString ( stringValueR ) , left . Type ) ;
483
486
}
484
- else if ( ( constantExpr = left as ConstantExpression ) != null && constantExpr . Value is string && ( typeConverter = TypeConverterFactory . GetConverter ( right . Type ) ) != null )
487
+ else if ( ( constantExpr = left as ConstantExpression ) != null && constantExpr . Value is string stringValueL && ( typeConverter = _typeConverterFactory . GetConverter ( right . Type ) ) != null )
485
488
{
486
- left = Expression . Constant ( typeConverter . ConvertFromInvariantString ( ( string ) constantExpr . Value ) , right . Type ) ;
489
+ left = Expression . Constant ( typeConverter . ConvertFromInvariantString ( stringValueL ) , right . Type ) ;
487
490
}
488
491
else
489
492
{
@@ -1526,7 +1529,7 @@ Expression ParseTypeAccess(Type type)
1526
1529
return ParseMemberAccess ( type , null ) ;
1527
1530
}
1528
1531
1529
- static Expression GenerateConversion ( Expression expr , Type type , int errorPos )
1532
+ private Expression GenerateConversion ( Expression expr , Type type , int errorPos )
1530
1533
{
1531
1534
Type exprType = expr . Type ;
1532
1535
if ( exprType == type )
@@ -1557,21 +1560,11 @@ static Expression GenerateConversion(Expression expr, Type type, int errorPos)
1557
1560
{
1558
1561
string text = ( string ) ( ( ConstantExpression ) expr ) . Value ;
1559
1562
1560
- // DateTime is parsed as UTC time.
1561
- if ( type == typeof ( DateTime ) && DateTime . TryParse ( text , CultureInfo . InvariantCulture , DateTimeStyles . None , out var dateTime ) )
1563
+ var typeConvertor = _typeConverterFactory . GetConverter ( type ) ;
1564
+ if ( typeConvertor != null )
1562
1565
{
1563
- return Expression . Constant ( dateTime , type ) ;
1564
- }
1565
-
1566
- object [ ] arguments = { text , null } ;
1567
- #if NETFX_CORE || WINDOWS_APP || DOTNET5_1 || UAP10_0 || NETSTANDARD
1568
- MethodInfo method = type . GetMethod ( "TryParse" , new [ ] { typeof ( string ) , type . MakeByRefType ( ) } ) ;
1569
- #else
1570
- MethodInfo method = type . GetMethod ( "TryParse" , BindingFlags . Public | BindingFlags . Static , null , new Type [ ] { typeof ( string ) , type . MakeByRefType ( ) } , null ) ;
1571
- #endif
1572
- if ( method != null && ( bool ) method . Invoke ( null , arguments ) )
1573
- {
1574
- return Expression . Constant ( arguments [ 1 ] , type ) ;
1566
+ var value = typeConvertor . ConvertFromInvariantString ( text ) ;
1567
+ return Expression . Constant ( value , type ) ;
1575
1568
}
1576
1569
}
1577
1570
0 commit comments