@@ -99,7 +99,9 @@ public virtual HashSet<Type> GetCustomTypes()
99
99
return _customTypes ;
100
100
}
101
101
102
- _customTypes = new HashSet < Type > ( FindTypesMarkedWithDynamicLinqTypeAttribute ( new [ ] { GetType ( ) . GetTypeInfo ( ) . Assembly } ) ) ;
102
+ _customTypes =
103
+ new HashSet < Type > (
104
+ FindTypesMarkedWithDynamicLinqTypeAttribute ( new [ ] { GetType ( ) . GetTypeInfo ( ) . Assembly } ) ) ;
103
105
_customTypes . Add ( typeof ( CustomClassWithStaticMethod ) ) ;
104
106
_customTypes . Add ( typeof ( StaticHelper ) ) ;
105
107
return _customTypes ;
@@ -111,6 +113,45 @@ public Type ResolveType(string typeName)
111
113
}
112
114
}
113
115
116
+ [ Fact ]
117
+ public void ParseLambda_UseParameterizedNamesInDynamicQuery_true ( )
118
+ {
119
+ // Assign
120
+ var config = new ParsingConfig
121
+ {
122
+ UseParameterizedNamesInDynamicQuery = true
123
+ } ;
124
+
125
+ // Act
126
+ var expression = DynamicExpressionParser . ParseLambda < string , bool > ( config , true , "s => s == \" x\" " ) ;
127
+
128
+ // Assert
129
+ dynamic constantExpression = ( ( MemberExpression ) ( expression . Body as BinaryExpression ) . Right ) . Expression as ConstantExpression ;
130
+ dynamic wrappedObj = constantExpression . Value ;
131
+ string value = wrappedObj . Value ;
132
+
133
+ Check . That ( value ) . IsEqualTo ( "x" ) ;
134
+ }
135
+
136
+ [ Fact ]
137
+ public void ParseLambda_UseParameterizedNamesInDynamicQuery_false ( )
138
+ {
139
+ // Assign
140
+ var config = new ParsingConfig
141
+ {
142
+ UseParameterizedNamesInDynamicQuery = false
143
+ } ;
144
+
145
+ // Act
146
+ var expression = DynamicExpressionParser . ParseLambda < string , bool > ( config , true , "s => s == \" x\" " ) ;
147
+
148
+ // Assert
149
+ dynamic constantExpression = ( ConstantExpression ) ( expression . Body as BinaryExpression ) . Right ;
150
+ string value = constantExpression . Value ;
151
+
152
+ Check . That ( value ) . IsEqualTo ( "x" ) ;
153
+ }
154
+
114
155
[ Fact ]
115
156
public void ParseLambda_ToList ( )
116
157
{
@@ -141,16 +182,20 @@ public void ParseLambda_Complex_1()
141
182
142
183
var externals = new Dictionary < string , object >
143
184
{
144
- { "Users" , qry }
185
+ { "Users" , qry }
145
186
} ;
146
187
147
188
// Act
148
- string query = "Users.GroupBy(x => new { x.Profile.Age }).OrderBy(gg => gg.Key.Age).Select(j => new (j.Key.Age, j.Sum(k => k.Income) As TotalIncome))" ;
189
+ string query =
190
+ "Users.GroupBy(x => new { x.Profile.Age }).OrderBy(gg => gg.Key.Age).Select(j => new (j.Key.Age, j.Sum(k => k.Income) As TotalIncome))" ;
149
191
LambdaExpression expression = DynamicExpressionParser . ParseLambda ( null , query , externals ) ;
150
192
Delegate del = expression . Compile ( ) ;
151
193
IEnumerable < dynamic > result = del . DynamicInvoke ( ) as IEnumerable < dynamic > ;
152
194
153
- var expected = qry . GroupBy ( x => new { x . Profile . Age } ) . OrderBy ( gg => gg . Key . Age ) . Select ( j => new { j . Key . Age , TotalIncome = j . Sum ( k => k . Income ) } ) . Select ( c => new ComplexParseLambda1Result { Age = c . Age , TotalIncome = c . TotalIncome } ) . Cast < dynamic > ( ) . ToArray ( ) ;
195
+ var expected = qry . GroupBy ( x => new { x . Profile . Age } ) . OrderBy ( gg => gg . Key . Age )
196
+ . Select ( j => new { j . Key . Age , TotalIncome = j . Sum ( k => k . Income ) } )
197
+ . Select ( c => new ComplexParseLambda1Result { Age = c . Age , TotalIncome = c . TotalIncome } ) . Cast < dynamic > ( )
198
+ . ToArray ( ) ;
154
199
155
200
// Assert
156
201
Check . That ( result ) . IsNotNull ( ) ;
@@ -166,12 +211,16 @@ public void ParseLambda_Complex_2()
166
211
var qry = testList . AsQueryable ( ) ;
167
212
168
213
// Act
169
- string query = "GroupBy(x => new { x.Profile.Age }, it).OrderBy(gg => gg.Key.Age).Select(j => new (j.Key.Age, j.Sum(k => k.Income) As TotalIncome))" ;
214
+ string query =
215
+ "GroupBy(x => new { x.Profile.Age }, it).OrderBy(gg => gg.Key.Age).Select(j => new (j.Key.Age, j.Sum(k => k.Income) As TotalIncome))" ;
170
216
LambdaExpression expression = DynamicExpressionParser . ParseLambda ( qry . GetType ( ) , null , query ) ;
171
217
Delegate del = expression . Compile ( ) ;
172
218
IEnumerable < dynamic > result = del . DynamicInvoke ( qry ) as IEnumerable < dynamic > ;
173
219
174
- var expected = qry . GroupBy ( x => new { x . Profile . Age } , x => x ) . OrderBy ( gg => gg . Key . Age ) . Select ( j => new { j . Key . Age , TotalIncome = j . Sum ( k => k . Income ) } ) . Select ( c => new ComplexParseLambda1Result { Age = c . Age , TotalIncome = c . TotalIncome } ) . Cast < dynamic > ( ) . ToArray ( ) ;
220
+ var expected = qry . GroupBy ( x => new { x . Profile . Age } , x => x ) . OrderBy ( gg => gg . Key . Age )
221
+ . Select ( j => new { j . Key . Age , TotalIncome = j . Sum ( k => k . Income ) } )
222
+ . Select ( c => new ComplexParseLambda1Result { Age = c . Age , TotalIncome = c . TotalIncome } ) . Cast < dynamic > ( )
223
+ . ToArray ( ) ;
175
224
176
225
// Assert
177
226
Check . That ( result ) . IsNotNull ( ) ;
@@ -197,8 +246,10 @@ public void ParseLambda_Complex_3()
197
246
} ;
198
247
199
248
// Act
200
- string stringExpression = "Users.GroupBy(x => new { x.Profile.Age }).OrderBy(gg => gg.Key.Age).Select(j => new System.Linq.Dynamic.Core.Tests.DynamicExpressionParserTests+ComplexParseLambda3Result{j.Key.Age, j.Sum(k => k.Income) As TotalIncome})" ;
201
- LambdaExpression expression = DynamicExpressionParser . ParseLambda ( config , null , stringExpression , externals ) ;
249
+ string stringExpression =
250
+ "Users.GroupBy(x => new { x.Profile.Age }).OrderBy(gg => gg.Key.Age).Select(j => new System.Linq.Dynamic.Core.Tests.DynamicExpressionParserTests+ComplexParseLambda3Result{j.Key.Age, j.Sum(k => k.Income) As TotalIncome})" ;
251
+ LambdaExpression expression =
252
+ DynamicExpressionParser . ParseLambda ( config , null , stringExpression , externals ) ;
202
253
Delegate del = expression . Compile ( ) ;
203
254
IEnumerable < dynamic > result = del . DynamicInvoke ( ) as IEnumerable < dynamic > ;
204
255
@@ -262,13 +313,14 @@ public void ParseLambda_4_Issue58()
262
313
{
263
314
var expressionParams = new [ ]
264
315
{
265
- Expression . Parameter ( typeof ( MyClass ) , "myObj" )
316
+ Expression . Parameter ( typeof ( MyClass ) , "myObj" )
266
317
} ;
267
318
268
319
var myClassInstance = new MyClass ( ) ;
269
320
var invokersMerge = new List < object > { myClassInstance } ;
270
321
271
- LambdaExpression expression = DynamicExpressionParser . ParseLambda ( false , expressionParams , null , "myObj.Foo()" ) ;
322
+ LambdaExpression expression =
323
+ DynamicExpressionParser . ParseLambda ( false , expressionParams , null , "myObj.Foo()" ) ;
272
324
Delegate del = expression . Compile ( ) ;
273
325
object result = del . DynamicInvoke ( invokersMerge . ToArray ( ) ) ;
274
326
@@ -385,22 +437,25 @@ public void ParseLambda_RealNumbers()
385
437
[ Fact ]
386
438
public void ParseLambda_StringLiteral_ReturnsBooleanLambdaExpression ( )
387
439
{
388
- var expression = DynamicExpressionParser . ParseLambda ( new [ ] { Expression . Parameter ( typeof ( string ) , "Property1" ) } , typeof ( bool ) , "Property1 == \" test\" " ) ;
440
+ var expression = DynamicExpressionParser . ParseLambda (
441
+ new [ ] { Expression . Parameter ( typeof ( string ) , "Property1" ) } , typeof ( bool ) , "Property1 == \" test\" " ) ;
389
442
Assert . Equal ( typeof ( bool ) , expression . Body . Type ) ;
390
443
}
391
444
392
445
[ Fact ]
393
446
public void ParseLambda_StringLiteralEmpty_ReturnsBooleanLambdaExpression ( )
394
447
{
395
- var expression = DynamicExpressionParser . ParseLambda ( new [ ] { Expression . Parameter ( typeof ( string ) , "Property1" ) } , typeof ( bool ) , "Property1 == \" \" " ) ;
448
+ var expression = DynamicExpressionParser . ParseLambda (
449
+ new [ ] { Expression . Parameter ( typeof ( string ) , "Property1" ) } , typeof ( bool ) , "Property1 == \" \" " ) ;
396
450
Assert . Equal ( typeof ( bool ) , expression . Body . Type ) ;
397
451
}
398
452
399
453
[ Fact ]
400
454
public void ParseLambda_Config_StringLiteralEmpty_ReturnsBooleanLambdaExpression ( )
401
455
{
402
456
var config = new ParsingConfig ( ) ;
403
- var expression = DynamicExpressionParser . ParseLambda ( new [ ] { Expression . Parameter ( typeof ( string ) , "Property1" ) } , typeof ( bool ) , "Property1 == \" \" " ) ;
457
+ var expression = DynamicExpressionParser . ParseLambda (
458
+ new [ ] { Expression . Parameter ( typeof ( string ) , "Property1" ) } , typeof ( bool ) , "Property1 == \" \" " ) ;
404
459
Assert . Equal ( typeof ( bool ) , expression . Body . Type ) ;
405
460
}
406
461
@@ -415,20 +470,9 @@ public void ParseLambda_StringLiteralEmbeddedQuote_ReturnsBooleanLambdaExpressio
415
470
typeof ( bool ) ,
416
471
string . Format ( "Property1 == {0}" , expectedRightValue ) ) ;
417
472
418
- var notWrappedExpression = DynamicExpressionParser . ParseLambda (
419
- new [ ] { Expression . Parameter ( typeof ( string ) , "Property1" ) } ,
420
- typeof ( bool ) ,
421
- string . Format ( "Property1 == {0} + \" \" " , expectedRightValue ) ) ;
422
-
423
- var notWrappedExpectedRightValue = ( ( ConstantExpression ) ( ( MethodCallExpression ) ( ( BinaryExpression ) notWrappedExpression . Body ) . Right ) . Arguments [ 0 ] ) . Value ;
424
-
425
- var constantExpression = ( ConstantExpression ) ( ( MemberExpression ) ( ( BinaryExpression ) expression . Body ) . Right ) . Expression ;
426
- dynamic wrappedObj = constantExpression . Value ;
427
-
428
- // Assert
429
- string rightValue = wrappedObj . Value ;
473
+ string rightValue = ( ( BinaryExpression ) expression . Body ) . Right . ToString ( ) ;
430
474
Assert . Equal ( typeof ( bool ) , expression . Body . Type ) ;
431
- Assert . Equal ( notWrappedExpectedRightValue , rightValue ) ;
475
+ Assert . Equal ( expectedRightValue , rightValue ) ;
432
476
}
433
477
434
478
[ Fact ]
@@ -442,19 +486,9 @@ public void ParseLambda_StringLiteralStartEmbeddedQuote_ReturnsBooleanLambdaExpr
442
486
typeof ( bool ) ,
443
487
string . Format ( "Property1 == {0}" , expectedRightValue ) ) ;
444
488
445
- var notWrappedExpression = DynamicExpressionParser . ParseLambda (
446
- new [ ] { Expression . Parameter ( typeof ( string ) , "Property1" ) } ,
447
- typeof ( bool ) ,
448
- string . Format ( "Property1 == {0} + \" \" " , expectedRightValue ) ) ;
449
-
450
- var notWrappedExpectedRightValue = ( ( ConstantExpression ) ( ( MethodCallExpression ) ( ( BinaryExpression ) notWrappedExpression . Body ) . Right ) . Arguments [ 0 ] ) . Value ;
451
-
452
- var constantExpression = ( ConstantExpression ) ( ( MemberExpression ) ( ( BinaryExpression ) expression . Body ) . Right ) . Expression ;
453
- dynamic wrappedObj = constantExpression . Value ;
454
-
455
- string rightValue = wrappedObj . Value ;
489
+ string rightValue = ( ( BinaryExpression ) expression . Body ) . Right . ToString ( ) ;
456
490
Assert . Equal ( typeof ( bool ) , expression . Body . Type ) ;
457
- Assert . Equal ( notWrappedExpectedRightValue , rightValue ) ;
491
+ Assert . Equal ( expectedRightValue , rightValue ) ;
458
492
}
459
493
460
494
[ Fact ]
@@ -480,20 +514,9 @@ public void ParseLambda_StringLiteralEscapedBackslash_ReturnsBooleanLambdaExpres
480
514
typeof ( bool ) ,
481
515
string . Format ( "Property1 == {0}" , expectedRightValue ) ) ;
482
516
483
- var notWrappedExpression = DynamicExpressionParser . ParseLambda (
484
- new [ ] { Expression . Parameter ( typeof ( string ) , "Property1" ) } ,
485
- typeof ( bool ) ,
486
- string . Format ( "Property1 == {0} + \" \" " , expectedRightValue ) ) ;
487
-
488
- var notWrappedExpectedRightValue = ( ( ConstantExpression ) ( ( MethodCallExpression ) ( ( BinaryExpression ) notWrappedExpression . Body ) . Right ) . Arguments [ 0 ] ) . Value ;
489
-
490
- var constantExpression = ( ConstantExpression ) ( ( MemberExpression ) ( ( BinaryExpression ) expression . Body ) . Right ) . Expression ;
491
- dynamic wrappedObj = constantExpression . Value ;
492
-
493
- // Assert
494
- string rightValue = wrappedObj . Value ;
495
- Assert . Equal ( typeof ( bool ) , expression . Body . Type ) ;
496
- Assert . Equal ( notWrappedExpectedRightValue , rightValue ) ;
517
+ string rightValue = ( ( BinaryExpression ) expression . Body ) . Right . ToString ( ) ;
518
+ Assert . Equal ( typeof ( Boolean ) , expression . Body . Type ) ;
519
+ Assert . Equal ( expectedRightValue , rightValue ) ;
497
520
}
498
521
499
522
[ Fact ]
@@ -509,11 +532,8 @@ public void ParseLambda_TupleToStringMethodCall_ReturnsStringLambdaExpression()
509
532
[ Fact ]
510
533
public void ParseLambda_IllegalMethodCall_ThrowsException ( )
511
534
{
512
- Check . ThatCode ( ( ) =>
513
- {
514
- DynamicExpressionParser . ParseLambda ( typeof ( IO . FileStream ) , null , "it.Close()" ) ;
515
- } )
516
- . Throws < ParseException > ( ) . WithMessage ( "Methods on type 'Stream' are not accessible" ) ;
535
+ Check . ThatCode ( ( ) => { DynamicExpressionParser . ParseLambda ( typeof ( IO . FileStream ) , null , "it.Close()" ) ; } )
536
+ . Throws < ParseException > ( ) . WithMessage ( "Methods on type 'Stream' are not accessible" ) ;
517
537
}
518
538
519
539
[ Fact ]
@@ -526,10 +546,12 @@ public void ParseLambda_CustomMethod()
526
546
} ;
527
547
528
548
var context = new CustomClassWithStaticMethod ( ) ;
529
- string expression = $ "{ nameof ( CustomClassWithStaticMethod ) } .{ nameof ( CustomClassWithStaticMethod . GetAge ) } (10)";
549
+ string expression =
550
+ $ "{ nameof ( CustomClassWithStaticMethod ) } .{ nameof ( CustomClassWithStaticMethod . GetAge ) } (10)";
530
551
531
552
// Act
532
- var lambdaExpression = DynamicExpressionParser . ParseLambda ( config , typeof ( CustomClassWithStaticMethod ) , null , expression ) ;
553
+ var lambdaExpression =
554
+ DynamicExpressionParser . ParseLambda ( config , typeof ( CustomClassWithStaticMethod ) , null , expression ) ;
533
555
Delegate del = lambdaExpression . Compile ( ) ;
534
556
int result = ( int ) del . DynamicInvoke ( context ) ;
535
557
@@ -600,7 +622,8 @@ public void ParseLambda_With_Guid_Equals_String()
600
622
var user = new User ( ) ;
601
623
user . Id = someId ;
602
624
Guid guidEmpty = Guid . Empty ;
603
- string expressionText = $ "iif(@0.Id == \" { someId } \" , Guid.Parse(\" { guidEmpty } \" ), Guid.Parse(\" { anotherId } \" ))";
625
+ string expressionText =
626
+ $ "iif(@0.Id == \" { someId } \" , Guid.Parse(\" { guidEmpty } \" ), Guid.Parse(\" { anotherId } \" ))";
604
627
605
628
// Act
606
629
var lambda = DynamicExpressionParser . ParseLambda ( typeof ( User ) , null , expressionText , user ) ;
@@ -676,7 +699,8 @@ public void ParseLambda_Operator_Less_Greater_With_Guids()
676
699
var user = new User ( ) ;
677
700
user . Id = someId ;
678
701
Guid guidEmpty = Guid . Empty ;
679
- string expressionText = $ "iif(@0.Id == StaticHelper.GetGuid(\" name\" ), Guid.Parse(\" { guidEmpty } \" ), Guid.Parse(\" { anotherId } \" ))";
702
+ string expressionText =
703
+ $ "iif(@0.Id == StaticHelper.GetGuid(\" name\" ), Guid.Parse(\" { guidEmpty } \" ), Guid.Parse(\" { anotherId } \" ))";
680
704
681
705
// Act
682
706
var lambda = DynamicExpressionParser . ParseLambda ( config , typeof ( User ) , null , expressionText , user ) ;
0 commit comments