@@ -1427,6 +1427,32 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx
1427
1427
if ( type == null )
1428
1428
throw new ExpressionEvaluatorSyntaxErrorException ( $ "Type or class { completeName } { genericTypes } is unknown") ;
1429
1429
1430
+ void Init ( object element , List < string > initArgs )
1431
+ {
1432
+ if ( typeof ( IEnumerable ) . IsAssignableFrom ( type ) && ! typeof ( IDictionary ) . IsAssignableFrom ( type ) )
1433
+ {
1434
+ MethodInfo methodInfo = type . GetMethod ( "Add" , BindingFlags . Public | BindingFlags . Instance ) ;
1435
+
1436
+ initArgs . ForEach ( subExpr => methodInfo . Invoke ( element , new object [ ] { Evaluate ( subExpr ) } ) ) ;
1437
+ }
1438
+ else
1439
+ {
1440
+ ExpressionEvaluator initEvaluator = new ExpressionEvaluator ( new Dictionary < string , object > ( ) { { "this" , element } } ) ;
1441
+
1442
+ initArgs . ForEach ( subExpr =>
1443
+ {
1444
+ if ( subExpr . Contains ( "=" ) )
1445
+ {
1446
+ string trimmedSubExpr = subExpr . TrimStart ( ) ;
1447
+
1448
+ initEvaluator . Evaluate ( $ "this{ ( trimmedSubExpr . StartsWith ( "[" ) ? string . Empty : "." ) } { trimmedSubExpr } ") ;
1449
+ }
1450
+ else
1451
+ throw new ExpressionEvaluatorSyntaxErrorException ( $ "A '=' char is missing in [{ subExpr } ]. It is in a object initializer. It must contains one.") ;
1452
+ } ) ;
1453
+ }
1454
+ }
1455
+
1430
1456
if ( instanceCreationMatch . Groups [ "isfunction" ] . Success )
1431
1457
{
1432
1458
List < string > constructorArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( expr , ref i , true ) ;
@@ -1444,28 +1470,7 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx
1444
1470
1445
1471
List < string > initArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( expr , ref i , true , "," , "{" , "}" ) ;
1446
1472
1447
- if ( typeof ( IEnumerable ) . IsAssignableFrom ( type ) && ! typeof ( IDictionary ) . IsAssignableFrom ( type ) )
1448
- {
1449
- MethodInfo methodInfo = type . GetMethod ( "Add" , BindingFlags . Public | BindingFlags . Instance ) ;
1450
-
1451
- initArgs . ForEach ( subExpr => methodInfo . Invoke ( element , new object [ ] { Evaluate ( subExpr ) } ) ) ;
1452
- }
1453
- else
1454
- {
1455
- ExpressionEvaluator initEvaluator = new ExpressionEvaluator ( new Dictionary < string , object > ( ) { { "this" , element } } ) ;
1456
-
1457
- initArgs . ForEach ( subExpr =>
1458
- {
1459
- if ( subExpr . Contains ( "=" ) )
1460
- {
1461
- string trimmedSubExpr = subExpr . TrimStart ( ) ;
1462
-
1463
- initEvaluator . Evaluate ( $ "this{ ( trimmedSubExpr . StartsWith ( "[" ) ? string . Empty : "." ) } { trimmedSubExpr } ") ;
1464
- }
1465
- else
1466
- throw new ExpressionEvaluatorSyntaxErrorException ( $ "A '=' char is missing in [{ subExpr } ]. It is in a object initializer. It must contains one.") ;
1467
- } ) ;
1468
- }
1473
+ Init ( element , initArgs ) ;
1469
1474
}
1470
1475
else
1471
1476
i -- ;
@@ -1474,33 +1479,11 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx
1474
1479
}
1475
1480
else if ( instanceCreationMatch . Groups [ "isInit" ] . Success )
1476
1481
{
1477
- List < string > initArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( expr , ref i , true , "," , "{" , "}" ) ;
1478
-
1479
1482
object element = Activator . CreateInstance ( type , new object [ 0 ] ) ;
1480
1483
1481
- if ( typeof ( IEnumerable ) . IsAssignableFrom ( type ) && ! typeof ( IDictionary ) . IsAssignableFrom ( type ) )
1482
- {
1483
- MethodInfo methodInfo = type . GetMethod ( "Add" , BindingFlags . Public | BindingFlags . Instance ) ;
1484
-
1485
- initArgs . ForEach ( subExpr => methodInfo . Invoke ( element , new object [ ] { Evaluate ( subExpr ) } ) ) ;
1486
- }
1487
- else
1488
- {
1489
- ExpressionEvaluator initEvaluator = new ExpressionEvaluator ( new Dictionary < string , object > ( ) { { "this" , element } } ) ;
1490
-
1491
- initArgs . ForEach ( subExpr =>
1492
- {
1493
- if ( subExpr . Contains ( "=" ) )
1494
- {
1495
- string trimmedSubExpr = subExpr . TrimStart ( ) ;
1496
-
1497
- initEvaluator . Evaluate ( $ "this{ ( trimmedSubExpr . StartsWith ( "[" ) ? string . Empty : "." ) } { trimmedSubExpr } ") ;
1498
- }
1499
- else
1500
- throw new ExpressionEvaluatorSyntaxErrorException ( $ "A '=' char is missing in [{ subExpr } ]. It is in a object initializer. It must contains one.") ;
1501
- } ) ;
1502
- }
1484
+ List < string > initArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( expr , ref i , true , "," , "{" , "}" ) ;
1503
1485
1486
+ Init ( element , initArgs ) ;
1504
1487
1505
1488
stack . Push ( element ) ;
1506
1489
}
0 commit comments