1
- using System ;
1
+ using System ;
2
2
using System . Collections . Generic ;
3
3
using System . ComponentModel ;
4
4
using System . Globalization ;
9
9
/// <summary>
10
10
/// This class allow to evaluate a string math or pseudo C# expression
11
11
/// </summary>
12
- internal class ExpressionEvaluator
12
+ public class ExpressionEvaluator
13
13
{
14
14
private static Regex varOrFunctionRegEx = new Regex ( @"^(?<inObject>(?<nullConditional>[?])?\.)?(?<name>[a-zA-Z_][a-zA-Z0-9_]*)\s*(?<isfunction>[(])?" , RegexOptions . IgnoreCase ) ;
15
15
private static Regex numberRegex = new Regex ( @"^(?<sign>[+-])?\d+(?<hasdecimal>\.?\d+(e[+-]?\d+)?)?(?<type>ul|[fdulm])?" , RegexOptions . IgnoreCase ) ;
@@ -182,7 +182,7 @@ private enum ExpressionOperator
182
182
{ ExpressionOperator . Greater , ( dynamic left , dynamic right ) => left > right } ,
183
183
{ ExpressionOperator . LowerOrEqual , ( dynamic left , dynamic right ) => left <= right } ,
184
184
{ 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 ( ) ) } ,
186
186
} ,
187
187
new Dictionary < ExpressionOperator , Func < dynamic , dynamic , object > > ( )
188
188
{
@@ -299,10 +299,16 @@ private enum ExpressionOperator
299
299
typeof ( Enumerable ) // For Linq extension methods
300
300
} ;
301
301
302
+ private Dictionary < string , object > variables = new Dictionary < string , object > ( StringComparer . OrdinalIgnoreCase ) ;
303
+
302
304
/// <summary>
303
305
/// The Values of the variable use in the expressions
304
306
/// </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
+ }
306
312
307
313
/// <summary>
308
314
/// 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()
322
328
/// <param name="variables">The Values of the variable use in the expressions</param>
323
329
public ExpressionEvaluator ( Dictionary < string , object > variables )
324
330
{
325
- this . Variables = variables ;
331
+ Variables = variables ;
326
332
}
327
333
328
334
/// <summary>
@@ -550,7 +556,7 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
550
556
}
551
557
else
552
558
{
553
- VariableEvaluationEventArg variableEvaluationEventArg = new VariableEvaluationEventArg ( completeVar ) ;
559
+ VariableEvaluationEventArg variableEvaluationEventArg = new VariableEvaluationEventArg ( var ) ;
554
560
555
561
EvaluateVariable ? . Invoke ( this , variableEvaluationEventArg ) ;
556
562
@@ -600,7 +606,7 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
600
606
601
607
if ( staticType != null )
602
608
{
603
- stack . Push ( staticType ) ;
609
+ stack . Push ( new ClassOrTypeName ( ) { Type = staticType } ) ;
604
610
}
605
611
else
606
612
{
@@ -1045,9 +1051,9 @@ private MethodInfo MakeConcreteMethodIfGeneric(MethodInfo methodInfo)
1045
1051
1046
1052
private BindingFlags DetermineInstanceOrStatic ( ref Type objType , ref object obj )
1047
1053
{
1048
- if ( obj is Type )
1054
+ if ( obj is ClassOrTypeName classOrTypeName )
1049
1055
{
1050
- objType = obj as Type ;
1056
+ objType = classOrTypeName . Type ;
1051
1057
obj = null ;
1052
1058
return staticBindingFlag ;
1053
1059
}
@@ -1243,6 +1249,11 @@ private string GetCodeUntilEndOfStringInterpolation(string subExpr)
1243
1249
return result ;
1244
1250
}
1245
1251
1252
+ private class ClassOrTypeName
1253
+ {
1254
+ public Type Type { get ; set ; }
1255
+ }
1256
+
1246
1257
private class DelegateEncaps
1247
1258
{
1248
1259
private lambdaExpressionDelegate lambda ;
@@ -1348,7 +1359,7 @@ public ExpressionEvaluatorSyntaxErrorException(string message, Exception innerEx
1348
1359
{ }
1349
1360
}
1350
1361
1351
- internal class VariableEvaluationEventArg : EventArgs
1362
+ public class VariableEvaluationEventArg : EventArgs
1352
1363
{
1353
1364
/// <summary>
1354
1365
///
@@ -1384,7 +1395,8 @@ public object Value
1384
1395
/// </summary>
1385
1396
public bool HasValue { get ; set ; } = false ;
1386
1397
}
1387
- internal class FunctionEvaluationEventArg : EventArgs
1398
+
1399
+ public class FunctionEvaluationEventArg : EventArgs
1388
1400
{
1389
1401
private Func < string , object > evaluateFunc = null ;
1390
1402
0 commit comments