@@ -137,6 +137,8 @@ public Expression GenerateEqual(Expression left, Expression right)
137
137
{
138
138
OptimizeForEqualityIfPossible ( ref left , ref right ) ;
139
139
140
+ TryConvertTypes ( ref left , ref right ) ;
141
+
140
142
WrapConstantExpressions ( ref left , ref right ) ;
141
143
142
144
return Expression . Equal ( left , right ) ;
@@ -146,16 +148,20 @@ public Expression GenerateNotEqual(Expression left, Expression right)
146
148
{
147
149
OptimizeForEqualityIfPossible ( ref left , ref right ) ;
148
150
151
+ TryConvertTypes ( ref left , ref right ) ;
152
+
149
153
WrapConstantExpressions ( ref left , ref right ) ;
150
154
151
155
return Expression . NotEqual ( left , right ) ;
152
156
}
153
157
154
158
public Expression GenerateGreaterThan ( Expression left , Expression right )
155
159
{
160
+ TryConvertTypes ( ref left , ref right ) ;
161
+
156
162
if ( left . Type == typeof ( string ) )
157
163
{
158
- return Expression . GreaterThan ( GenerateStaticMethodCall ( " Compare" , left , right ) , Expression . Constant ( 0 ) ) ;
164
+ return Expression . GreaterThan ( GenerateStaticMethodCall ( nameof ( string . Compare ) , left , right ) , Expression . Constant ( 0 ) ) ;
159
165
}
160
166
161
167
if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
@@ -172,9 +178,11 @@ public Expression GenerateGreaterThan(Expression left, Expression right)
172
178
173
179
public Expression GenerateGreaterThanEqual ( Expression left , Expression right )
174
180
{
181
+ TryConvertTypes ( ref left , ref right ) ;
182
+
175
183
if ( left . Type == typeof ( string ) )
176
184
{
177
- return Expression . GreaterThanOrEqual ( GenerateStaticMethodCall ( " Compare" , left , right ) , Expression . Constant ( 0 ) ) ;
185
+ return Expression . GreaterThanOrEqual ( GenerateStaticMethodCall ( nameof ( string . Compare ) , left , right ) , Expression . Constant ( 0 ) ) ;
178
186
}
179
187
180
188
if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
@@ -192,9 +200,11 @@ public Expression GenerateGreaterThanEqual(Expression left, Expression right)
192
200
193
201
public Expression GenerateLessThan ( Expression left , Expression right )
194
202
{
203
+ TryConvertTypes ( ref left , ref right ) ;
204
+
195
205
if ( left . Type == typeof ( string ) )
196
206
{
197
- return Expression . LessThan ( GenerateStaticMethodCall ( " Compare" , left , right ) , Expression . Constant ( 0 ) ) ;
207
+ return Expression . LessThan ( GenerateStaticMethodCall ( nameof ( string . Compare ) , left , right ) , Expression . Constant ( 0 ) ) ;
198
208
}
199
209
200
210
if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
@@ -212,9 +222,11 @@ public Expression GenerateLessThan(Expression left, Expression right)
212
222
213
223
public Expression GenerateLessThanEqual ( Expression left , Expression right )
214
224
{
225
+ TryConvertTypes ( ref left , ref right ) ;
226
+
215
227
if ( left . Type == typeof ( string ) )
216
228
{
217
- return Expression . LessThanOrEqual ( GenerateStaticMethodCall ( " Compare" , left , right ) , Expression . Constant ( 0 ) ) ;
229
+ return Expression . LessThanOrEqual ( GenerateStaticMethodCall ( nameof ( string . Compare ) , left , right ) , Expression . Constant ( 0 ) ) ;
218
230
}
219
231
220
232
if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
@@ -268,14 +280,14 @@ public void OptimizeForEqualityIfPossible(ref Expression left, ref Expression ri
268
280
#endif
269
281
270
282
#if ! NET35
271
- if ( type == typeof ( Guid ) && Guid . TryParse ( text , out Guid guid ) )
283
+ if ( type == typeof ( Guid ) && Guid . TryParse ( text , out var guid ) )
272
284
{
273
285
return Expression . Constant ( guid , typeof ( Guid ) ) ;
274
286
}
275
287
#else
276
288
try
277
289
{
278
- return Expression . Constant ( new Guid ( text ) ) ;
290
+ return Expression . Constant ( new Guid ( text ! ) ) ;
279
291
}
280
292
catch
281
293
{
@@ -399,7 +411,7 @@ private List<Expression> CollectExpressions(bool addSelf, Expression sourceExpre
399
411
{
400
412
switch ( expression )
401
413
{
402
- case MemberExpression _ :
414
+ case MemberExpression :
403
415
list . Add ( sourceExpression ) ;
404
416
break ;
405
417
@@ -443,6 +455,26 @@ private List<Expression> CollectExpressions(bool addSelf, Expression sourceExpre
443
455
return list ;
444
456
}
445
457
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
+
446
478
private static Expression GenerateStaticMethodCall ( string methodName , Expression left , Expression right )
447
479
{
448
480
return Expression . Call ( null , GetStaticMethod ( methodName , left , right ) , new [ ] { left , right } ) ;
0 commit comments