Skip to content

Commit 73ee598

Browse files
authored
force given custom variables to be evaluated with ignore case
1 parent 8b5d2de commit 73ee598

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

ExpressionEvaluator.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
44
using System.Globalization;
@@ -9,7 +9,7 @@
99
/// <summary>
1010
/// This class allow to evaluate a string math or pseudo C# expression
1111
/// </summary>
12-
internal class ExpressionEvaluator
12+
public class ExpressionEvaluator
1313
{
1414
private static Regex varOrFunctionRegEx = new Regex(@"^(?<inObject>(?<nullConditional>[?])?\.)?(?<name>[a-zA-Z_][a-zA-Z0-9_]*)\s*(?<isfunction>[(])?", RegexOptions.IgnoreCase);
1515
private static Regex numberRegex = new Regex(@"^(?<sign>[+-])?\d+(?<hasdecimal>\.?\d+(e[+-]?\d+)?)?(?<type>ul|[fdulm])?", RegexOptions.IgnoreCase);
@@ -182,7 +182,7 @@ private enum ExpressionOperator
182182
{ExpressionOperator.Greater, (dynamic left, dynamic right) => left > right },
183183
{ExpressionOperator.LowerOrEqual, (dynamic left, dynamic right) => left <= right },
184184
{ExpressionOperator.GreaterOrEqual, (dynamic left, dynamic right) => left >= right },
185-
{ExpressionOperator.Is, (dynamic left, dynamic right) => ((Type)right).IsAssignableFrom(left.GetType()) },
185+
{ExpressionOperator.Is, (dynamic left, dynamic right) => (((ClassOrTypeName)right).Type).IsAssignableFrom(left.GetType()) },
186186
},
187187
new Dictionary<ExpressionOperator, Func<dynamic, dynamic, object>>()
188188
{
@@ -299,10 +299,16 @@ private enum ExpressionOperator
299299
typeof(Enumerable) // For Linq extension methods
300300
};
301301

302+
private Dictionary<string, object> variables = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
303+
302304
/// <summary>
303305
/// The Values of the variable use in the expressions
304306
/// </summary>
305-
public Dictionary<string, object> Variables { get; set; } = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
307+
public Dictionary<string, object> Variables
308+
{
309+
get { return variables; }
310+
set { variables = new Dictionary<string, object>(value, StringComparer.OrdinalIgnoreCase); }
311+
}
306312

307313
/// <summary>
308314
/// If <c>true</c> Evaluate function is callables in an expression. If <c>false</c> Evaluate is not callable.
@@ -322,7 +328,7 @@ public ExpressionEvaluator()
322328
/// <param name="variables">The Values of the variable use in the expressions</param>
323329
public ExpressionEvaluator(Dictionary<string, object> variables)
324330
{
325-
this.Variables = variables;
331+
Variables = variables;
326332
}
327333

328334
/// <summary>
@@ -550,7 +556,7 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
550556
}
551557
else
552558
{
553-
VariableEvaluationEventArg variableEvaluationEventArg = new VariableEvaluationEventArg(completeVar);
559+
VariableEvaluationEventArg variableEvaluationEventArg = new VariableEvaluationEventArg(var);
554560

555561
EvaluateVariable?.Invoke(this, variableEvaluationEventArg);
556562

@@ -600,7 +606,7 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
600606

601607
if (staticType != null)
602608
{
603-
stack.Push(staticType);
609+
stack.Push(new ClassOrTypeName() { Type = staticType });
604610
}
605611
else
606612
{
@@ -1045,9 +1051,9 @@ private MethodInfo MakeConcreteMethodIfGeneric(MethodInfo methodInfo)
10451051

10461052
private BindingFlags DetermineInstanceOrStatic(ref Type objType, ref object obj)
10471053
{
1048-
if (obj is Type)
1054+
if (obj is ClassOrTypeName classOrTypeName)
10491055
{
1050-
objType = obj as Type;
1056+
objType = classOrTypeName.Type;
10511057
obj = null;
10521058
return staticBindingFlag;
10531059
}
@@ -1243,6 +1249,11 @@ private string GetCodeUntilEndOfStringInterpolation(string subExpr)
12431249
return result;
12441250
}
12451251

1252+
private class ClassOrTypeName
1253+
{
1254+
public Type Type { get; set; }
1255+
}
1256+
12461257
private class DelegateEncaps
12471258
{
12481259
private lambdaExpressionDelegate lambda;
@@ -1348,7 +1359,7 @@ public ExpressionEvaluatorSyntaxErrorException(string message, Exception innerEx
13481359
{ }
13491360
}
13501361

1351-
internal class VariableEvaluationEventArg : EventArgs
1362+
public class VariableEvaluationEventArg : EventArgs
13521363
{
13531364
/// <summary>
13541365
///
@@ -1384,7 +1395,8 @@ public object Value
13841395
/// </summary>
13851396
public bool HasValue { get; set; } = false;
13861397
}
1387-
internal class FunctionEvaluationEventArg : EventArgs
1398+
1399+
public class FunctionEvaluationEventArg : EventArgs
13881400
{
13891401
private Func<string, object> evaluateFunc = null;
13901402

0 commit comments

Comments
 (0)