@@ -137,6 +137,8 @@ public Expression GenerateEqual(Expression left, Expression right)
137137 {
138138 OptimizeForEqualityIfPossible ( ref left , ref right ) ;
139139
140+ TryConvertTypes ( ref left , ref right ) ;
141+
140142 WrapConstantExpressions ( ref left , ref right ) ;
141143
142144 return Expression . Equal ( left , right ) ;
@@ -146,16 +148,20 @@ public Expression GenerateNotEqual(Expression left, Expression right)
146148 {
147149 OptimizeForEqualityIfPossible ( ref left , ref right ) ;
148150
151+ TryConvertTypes ( ref left , ref right ) ;
152+
149153 WrapConstantExpressions ( ref left , ref right ) ;
150154
151155 return Expression . NotEqual ( left , right ) ;
152156 }
153157
154158 public Expression GenerateGreaterThan ( Expression left , Expression right )
155159 {
160+ TryConvertTypes ( ref left , ref right ) ;
161+
156162 if ( left . Type == typeof ( string ) )
157163 {
158- return Expression . GreaterThan ( GenerateStaticMethodCall ( " Compare" , left , right ) , Expression . Constant ( 0 ) ) ;
164+ return Expression . GreaterThan ( GenerateStaticMethodCall ( nameof ( string . Compare ) , left , right ) , Expression . Constant ( 0 ) ) ;
159165 }
160166
161167 if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
@@ -172,9 +178,11 @@ public Expression GenerateGreaterThan(Expression left, Expression right)
172178
173179 public Expression GenerateGreaterThanEqual ( Expression left , Expression right )
174180 {
181+ TryConvertTypes ( ref left , ref right ) ;
182+
175183 if ( left . Type == typeof ( string ) )
176184 {
177- return Expression . GreaterThanOrEqual ( GenerateStaticMethodCall ( " Compare" , left , right ) , Expression . Constant ( 0 ) ) ;
185+ return Expression . GreaterThanOrEqual ( GenerateStaticMethodCall ( nameof ( string . Compare ) , left , right ) , Expression . Constant ( 0 ) ) ;
178186 }
179187
180188 if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
@@ -192,9 +200,11 @@ public Expression GenerateGreaterThanEqual(Expression left, Expression right)
192200
193201 public Expression GenerateLessThan ( Expression left , Expression right )
194202 {
203+ TryConvertTypes ( ref left , ref right ) ;
204+
195205 if ( left . Type == typeof ( string ) )
196206 {
197- return Expression . LessThan ( GenerateStaticMethodCall ( " Compare" , left , right ) , Expression . Constant ( 0 ) ) ;
207+ return Expression . LessThan ( GenerateStaticMethodCall ( nameof ( string . Compare ) , left , right ) , Expression . Constant ( 0 ) ) ;
198208 }
199209
200210 if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
@@ -212,9 +222,11 @@ public Expression GenerateLessThan(Expression left, Expression right)
212222
213223 public Expression GenerateLessThanEqual ( Expression left , Expression right )
214224 {
225+ TryConvertTypes ( ref left , ref right ) ;
226+
215227 if ( left . Type == typeof ( string ) )
216228 {
217- return Expression . LessThanOrEqual ( GenerateStaticMethodCall ( " Compare" , left , right ) , Expression . Constant ( 0 ) ) ;
229+ return Expression . LessThanOrEqual ( GenerateStaticMethodCall ( nameof ( string . Compare ) , left , right ) , Expression . Constant ( 0 ) ) ;
218230 }
219231
220232 if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
@@ -268,14 +280,14 @@ public void OptimizeForEqualityIfPossible(ref Expression left, ref Expression ri
268280#endif
269281
270282#if ! NET35
271- if ( type == typeof ( Guid ) && Guid . TryParse ( text , out Guid guid ) )
283+ if ( type == typeof ( Guid ) && Guid . TryParse ( text , out var guid ) )
272284 {
273285 return Expression . Constant ( guid , typeof ( Guid ) ) ;
274286 }
275287#else
276288 try
277289 {
278- return Expression . Constant ( new Guid ( text ) ) ;
290+ return Expression . Constant ( new Guid ( text ! ) ) ;
279291 }
280292 catch
281293 {
@@ -399,7 +411,7 @@ private List<Expression> CollectExpressions(bool addSelf, Expression sourceExpre
399411 {
400412 switch ( expression )
401413 {
402- case MemberExpression _ :
414+ case MemberExpression :
403415 list . Add ( sourceExpression ) ;
404416 break ;
405417
@@ -443,6 +455,26 @@ private List<Expression> CollectExpressions(bool addSelf, Expression sourceExpre
443455 return list ;
444456 }
445457
458+ /// <summary>
459+ /// If the types are different (and not null), try to convert the object type to other type.
460+ /// </summary>
461+ private void TryConvertTypes ( ref Expression left , ref Expression right )
462+ {
463+ if ( ! _parsingConfig . ConvertObjectToSupportComparison || left . Type == right . Type || Constants . IsNull ( left ) || Constants . IsNull ( right ) )
464+ {
465+ return ;
466+ }
467+
468+ if ( left . Type == typeof ( object ) )
469+ {
470+ left = Expression . Convert ( left , right . Type ) ;
471+ }
472+ else if ( right . Type == typeof ( object ) )
473+ {
474+ right = Expression . Convert ( right , left . Type ) ;
475+ }
476+ }
477+
446478 private static Expression GenerateStaticMethodCall ( string methodName , Expression left , Expression right )
447479 {
448480 return Expression . Call ( null , GetStaticMethod ( methodName , left , right ) , new [ ] { left , right } ) ;
0 commit comments