Skip to content

Commit c4954a3

Browse files
committed
Add events ExpressionEvaluating and ExpressionEvaluated
1 parent c273809 commit c4954a3

File tree

3 files changed

+93
-13
lines changed

3 files changed

+93
-13
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,6 @@ public void TypeTesting(string expression, Type type)
992992

993993
#endregion
994994

995-
996995
#endregion
997996
public object DirectExpressionEvaluation(string expression)
998997
{

CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
<PackageIconUrl>https://github.com/codingseb/ExpressionEvaluator/blob/master/Icon.png?raw=true</PackageIconUrl>
2020
<PackageIcon>Icon.png</PackageIcon>
2121
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
22-
<PackageReleaseNotes>* Add the option OptionCanDeclareMultiExpressionsLambdaInSimpleExpressionEvaluate (default is true)
23-
* Allow to declare and call multi expressions lambda in simple expression Evaluate</PackageReleaseNotes>
22+
<PackageReleaseNotes></PackageReleaseNotes>
2423
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
2524
<RepositoryUrl>https://github.com/codingseb/ExpressionEvaluator</RepositoryUrl>
2625
</PropertyGroup>

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 92 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,18 @@ public IDictionary<string, object> Variables
926926
set { variables = value == null ? new Dictionary<string, object>(StringComparerForCasing) : new Dictionary<string, object>(value, StringComparerForCasing); }
927927
}
928928

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+
929941
/// <summary>
930942
/// Is fired before a variable, field or property resolution.
931943
/// Allow to define a variable and the corresponding value on the fly.
@@ -1564,25 +1576,51 @@ public object Evaluate(string expression)
15641576

15651577
Stack<object> stack = new Stack<object>();
15661578
evaluationStackCount++;
1579+
object result;
1580+
15671581
try
15681582
{
1569-
if (GetLambdaExpression(expression, stack))
1570-
return stack.Pop();
1583+
ExpressionEvaluationEventArg expressionEvaluationEventArg = new ExpressionEvaluationEventArg(expression, this);
15711584

1572-
for (int i = 0; i < expression.Length; i++)
1585+
ExpressionEvaluating?.Invoke(this, expressionEvaluationEventArg);
1586+
1587+
expression = expressionEvaluationEventArg.Expression;
1588+
1589+
if (expressionEvaluationEventArg.HasValue)
15731590
{
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();
15771597

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)))
15791601
{
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+
}
15811608
}
15821609
}
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+
}
15831621
}
15841622

1585-
return ProcessStack(stack);
1623+
return result;
15861624
}
15871625
finally
15881626
{
@@ -3015,6 +3053,7 @@ protected virtual object ProcessStack(Stack<object> stack)
30153053

30163054
return stack.Pop();
30173055
}
3056+
30183057
#endregion
30193058

30203059
#region Remove comments
@@ -4292,6 +4331,50 @@ public Type[] EvaluateGenericTypes()
42924331
}
42934332
}
42944333

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+
42954378
/// <summary>
42964379
/// Infos about the variable, attribut or property that is currently evaluate
42974380
/// </summary>
@@ -4433,7 +4516,6 @@ public FunctionPreEvaluationEventArg(string name, List<string> args = null, Expr
44334516
public bool CancelEvaluation { get; set; }
44344517
}
44354518

4436-
44374519
/// <summary>
44384520
/// Infos about the indexing that is currently evaluate
44394521
/// </summary>

0 commit comments

Comments
 (0)