@@ -926,6 +926,18 @@ public IDictionary<string, object> Variables
926
926
set { variables = value == null ? new Dictionary < string , object > ( StringComparerForCasing ) : new Dictionary < string , object > ( value , StringComparerForCasing ) ; }
927
927
}
928
928
929
+ /// <summary>
930
+ /// Is fired just before an expression is evaluate.
931
+ /// Allow to redefine the expression to evaluate or to force a result value.
932
+ /// </summary>
933
+ public event EventHandler < ExpressionEvaluationEventArg > ExpressionEvaluating ;
934
+
935
+ /// <summary>
936
+ /// Is fired just before to return the expression evaluation.
937
+ /// Allow to modify on the fly the result of the evaluation.
938
+ /// </summary>
939
+ public event EventHandler < ExpressionEvaluationEventArg > ExpressionEvaluated ;
940
+
929
941
/// <summary>
930
942
/// Is fired before a variable, field or property resolution.
931
943
/// Allow to define a variable and the corresponding value on the fly.
@@ -1564,25 +1576,51 @@ public object Evaluate(string expression)
1564
1576
1565
1577
Stack < object > stack = new Stack < object > ( ) ;
1566
1578
evaluationStackCount ++ ;
1579
+ object result ;
1580
+
1567
1581
try
1568
1582
{
1569
- if ( GetLambdaExpression ( expression , stack ) )
1570
- return stack . Pop ( ) ;
1583
+ ExpressionEvaluationEventArg expressionEvaluationEventArg = new ExpressionEvaluationEventArg ( expression , this ) ;
1571
1584
1572
- for ( int i = 0 ; i < expression . Length ; i ++ )
1585
+ ExpressionEvaluating ? . Invoke ( this , expressionEvaluationEventArg ) ;
1586
+
1587
+ expression = expressionEvaluationEventArg . Expression ;
1588
+
1589
+ if ( expressionEvaluationEventArg . HasValue )
1573
1590
{
1574
- if ( ! ParsingMethods . Any ( parsingMethod => parsingMethod ( expression , stack , ref i ) ) )
1575
- {
1576
- string s = expression . Substring ( i , 1 ) ;
1591
+ result = expressionEvaluationEventArg . Value ;
1592
+ }
1593
+ else
1594
+ {
1595
+ if ( GetLambdaExpression ( expression , stack ) )
1596
+ return stack . Pop ( ) ;
1577
1597
1578
- if ( ! s . Trim ( ) . Equals ( string . Empty ) )
1598
+ for ( int i = 0 ; i < expression . Length ; i ++ )
1599
+ {
1600
+ if ( ! ParsingMethods . Any ( parsingMethod => parsingMethod ( expression , stack , ref i ) ) )
1579
1601
{
1580
- throw new ExpressionEvaluatorSyntaxErrorException ( $ "Invalid character [{ ( int ) s [ 0 ] } :{ s } ]") ;
1602
+ string s = expression . Substring ( i , 1 ) ;
1603
+
1604
+ if ( ! s . Trim ( ) . Equals ( string . Empty ) )
1605
+ {
1606
+ throw new ExpressionEvaluatorSyntaxErrorException ( $ "Invalid character [{ ( int ) s [ 0 ] } :{ s } ]") ;
1607
+ }
1581
1608
}
1582
1609
}
1610
+
1611
+ result = ProcessStack ( stack ) ;
1612
+
1613
+ expressionEvaluationEventArg = new ExpressionEvaluationEventArg ( expression , this , result ) ;
1614
+
1615
+ ExpressionEvaluated ? . Invoke ( this , expressionEvaluationEventArg ) ;
1616
+
1617
+ if ( expressionEvaluationEventArg . HasValue )
1618
+ {
1619
+ result = expressionEvaluationEventArg . Value ;
1620
+ }
1583
1621
}
1584
1622
1585
- return ProcessStack ( stack ) ;
1623
+ return result ;
1586
1624
}
1587
1625
finally
1588
1626
{
@@ -3015,6 +3053,7 @@ protected virtual object ProcessStack(Stack<object> stack)
3015
3053
3016
3054
return stack . Pop ( ) ;
3017
3055
}
3056
+
3018
3057
#endregion
3019
3058
3020
3059
#region Remove comments
@@ -4292,6 +4331,50 @@ public Type[] EvaluateGenericTypes()
4292
4331
}
4293
4332
}
4294
4333
4334
+ public partial class ExpressionEvaluationEventArg : EventArgs
4335
+ {
4336
+ private object value ;
4337
+
4338
+ public ExpressionEvaluationEventArg ( string expression , ExpressionEvaluator evaluator )
4339
+ {
4340
+ Expression = expression ;
4341
+ Evaluator = evaluator ;
4342
+ }
4343
+
4344
+ public ExpressionEvaluationEventArg ( string expression , ExpressionEvaluator evaluator , object value )
4345
+ {
4346
+ Expression = expression ;
4347
+ Evaluator = evaluator ;
4348
+ this . value = value ;
4349
+ }
4350
+
4351
+ public ExpressionEvaluator Evaluator { get ; }
4352
+
4353
+ /// <summary>
4354
+ /// The Expression that wil be evaluated.
4355
+ /// Can be modified.
4356
+ /// </summary>
4357
+ public string Expression { get ; set ; }
4358
+
4359
+ /// <summary>
4360
+ /// To set the return of the evaluation
4361
+ /// </summary>
4362
+ public object Value
4363
+ {
4364
+ get { return value ; }
4365
+ set
4366
+ {
4367
+ this . value = value ;
4368
+ HasValue = true ;
4369
+ }
4370
+ }
4371
+
4372
+ /// <summary>
4373
+ /// if <c>true</c> the expression evaluation has been done, if <c>false</c> it means that the evaluation must continue.
4374
+ /// </summary>
4375
+ public bool HasValue { get ; set ; }
4376
+ }
4377
+
4295
4378
/// <summary>
4296
4379
/// Infos about the variable, attribut or property that is currently evaluate
4297
4380
/// </summary>
@@ -4433,7 +4516,6 @@ public FunctionPreEvaluationEventArg(string name, List<string> args = null, Expr
4433
4516
public bool CancelEvaluation { get ; set ; }
4434
4517
}
4435
4518
4436
-
4437
4519
/// <summary>
4438
4520
/// Infos about the indexing that is currently evaluate
4439
4521
/// </summary>
0 commit comments