Skip to content

Commit 59a8cbd

Browse files
committed
Add Generic casting Evaluate method
1 parent e387a9a commit 59a8cbd

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -473,35 +473,46 @@ public ExpressionEvaluator(Dictionary<string, object> variables) : this()
473473
/// <summary>
474474
/// Evaluate the specified math or pseudo C# expression
475475
/// </summary>
476-
/// <param name="expr">the math or pseudo C# expression to evaluate</param>
476+
/// <typeparam name="T">The type in which to cast the result of the expression</typeparam>
477+
/// <param name="expression">the math or pseudo C# expression to evaluate</param>
478+
/// <returns>The result of the operation if syntax is correct casted in the specified type</returns>
479+
public T Evaluate<T>(string expression)
480+
{
481+
return (T)Evaluate(expression);
482+
}
483+
484+
/// <summary>
485+
/// Evaluate the specified math or pseudo C# expression
486+
/// </summary>
487+
/// <param name="expression">the math or pseudo C# expression to evaluate</param>
477488
/// <returns>The result of the operation if syntax is correct</returns>
478-
public object Evaluate(string expr)
489+
public object Evaluate(string expression)
479490
{
480491
bool continueEvaluation = true;
481492

482-
expr = expr.Trim();
493+
expression = expression.Trim();
483494

484495
Stack<object> stack = new Stack<object>();
485496

486-
if (GetLambdaExpression(expr, stack))
497+
if (GetLambdaExpression(expression, stack))
487498
return stack.Pop();
488499

489-
for (int i = 0; i < expr.Length && continueEvaluation; i++)
500+
for (int i = 0; i < expression.Length && continueEvaluation; i++)
490501
{
491-
string restOfExpression = expr.Substring(i, expr.Length - i);
502+
string restOfExpression = expression.Substring(i, expression.Length - i);
492503

493504
if (!(EvaluateCast(restOfExpression, stack, ref i)
494505
|| EvaluateNumber(restOfExpression, stack, ref i)
495-
|| EvaluateInstanceCreationWithNewKeyword(expr, restOfExpression, stack, ref i)
496-
|| EvaluateVarOrFunc(expr, restOfExpression, stack, ref i)
497-
|| EvaluateTwoCharsOperators(expr, stack, ref i)))
506+
|| EvaluateInstanceCreationWithNewKeyword(expression, restOfExpression, stack, ref i)
507+
|| EvaluateVarOrFunc(expression, restOfExpression, stack, ref i)
508+
|| EvaluateTwoCharsOperators(expression, stack, ref i)))
498509
{
499-
string s = expr.Substring(i, 1);
510+
string s = expression.Substring(i, 1);
500511

501-
if (EvaluateChar(expr, s, stack, ref i)
502-
|| EvaluateParenthis(expr, s, stack, ref i)
503-
|| EvaluateIndexing(expr, s, stack, ref i)
504-
|| EvaluateString(expr, s, restOfExpression, stack, ref i))
512+
if (EvaluateChar(expression, s, stack, ref i)
513+
|| EvaluateParenthis(expression, s, stack, ref i)
514+
|| EvaluateIndexing(expression, s, stack, ref i)
515+
|| EvaluateString(expression, s, restOfExpression, stack, ref i))
505516
{ }
506517
else if (operatorsDictionary.ContainsKey(s))
507518
{

0 commit comments

Comments
 (0)