@@ -1427,6 +1427,32 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx
14271427 if ( type == null )
14281428 throw new ExpressionEvaluatorSyntaxErrorException ( $ "Type or class { completeName } { genericTypes } is unknown") ;
14291429
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+
14301456 if ( instanceCreationMatch . Groups [ "isfunction" ] . Success )
14311457 {
14321458 List < string > constructorArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( expr , ref i , true ) ;
@@ -1444,28 +1470,7 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx
14441470
14451471 List < string > initArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( expr , ref i , true , "," , "{" , "}" ) ;
14461472
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 ) ;
14691474 }
14701475 else
14711476 i -- ;
@@ -1474,33 +1479,11 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx
14741479 }
14751480 else if ( instanceCreationMatch . Groups [ "isInit" ] . Success )
14761481 {
1477- List < string > initArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets ( expr , ref i , true , "," , "{" , "}" ) ;
1478-
14791482 object element = Activator . CreateInstance ( type , new object [ 0 ] ) ;
14801483
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 , "," , "{" , "}" ) ;
15031485
1486+ Init ( element , initArgs ) ;
15041487
15051488 stack . Push ( element ) ;
15061489 }
0 commit comments