0

Lets say i have

int a = 1;
int b = 2;
string exp = "b > a";

and i want to evaluate the string expression with those variables

if(exp.SomeKindOfParseOrCast())
{
   //here be magic
}

Is it possible in any simple way?

Szadi
  • 37
  • 3
  • I think Ncalc can do this. Not exactly like this but it can evaluate expressions. Get the library from nugget. – M.kazem Akhgary Dec 18 '15 at 23:42
  • Try something like `DynamicExpression.ParseLambda` like in [How to convert a String to its equivalent Expression Tree?](http://stackoverflow.com/questions/821365/) (uses a third-party library). Or maybe you do not even need to start with a string but can start directly from an expression tree? – Jeppe Stig Nielsen Dec 19 '15 at 19:05

4 Answers4

1

Nope, not in C# - these are parameter names, and thus are compile time values, and this expression parsing you are describing is done in runtime - the computer doesn't know the name of the parameters while it's being evaluated. Instead, you could do something a little more strict, like an expression parser - implement your own way to parse string expressions.

Very very simplified:

if(exp.Equals("b > a"))
{
 if(b>a)
  // do what you do if b is bigger than a
 else 
  // do what you do with a wrong expression
}
else if (exp.Equals("a > b")
{ 
 if(a>b)
  // do what you do if a is bigger than b
 else
  // do what you do with a wrong expression
}
else if (exp.Equals("a = b")
{
 if(a==b)
  // do what you do if a is equal to b
 else 
  // do what you do with a wrong expression
}
else
 // do what you do with a badly formatted expression

if you would like to take this a step forward, you can cut spaces, make sure the expression is lowercase, etc. - there's many examples around, I personally like this one.

A. Abramov
  • 1,823
  • 17
  • 45
  • Thanks for the engagement. Unfortunately, i need it to be generated dynamically, since the string might be pretty long and completely random, lets say "(a > 5 or b >=3) || c <= 2 && a > 7 || (b > 3 && c = 0)". What would you suggest in such case? – Szadi Dec 18 '15 at 23:10
  • @user3672414 Well, if that's all you're going to tell me, I'd suggest building a huge parser, because I can't see anything else. If you'll tell me what exacly you're trying to do, we might find a way around it. – A. Abramov Dec 18 '15 at 23:12
  • @OndrejTucny Of course, you take an expression, run a bunch of operations on it to trim it and fit it into a specific format, then you analyse it... but I'm not going to do all that just to illustrate this point. I believe the link I gave does that well enough. – A. Abramov Dec 18 '15 at 23:18
0

Is it possible in any simple way?

No, in C# this is not possible in a simple way like it were in languages such as JavaScript with its eval function. Anyway, you'd have to provide bindings of in-expression parameters to actual values.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
  • What about the Roslyn? I do not know Roslyn, but I've seen that it can compile and use the code at runtime. – BWA Dec 19 '15 at 15:14
  • @Karol I think Roslyn is overkill here. By compiling and executing you suddenly face bunch of security issues. NCalc or some other expression interpreter is a safer bet. – Ondrej Tucny Dec 19 '15 at 16:16
  • You're right, but this is one possible solution. It is possible that in the future needs of the author and interpreter will be increased expressions will not suffice. Or not;-) best solution depends on the specific needs of the author. – BWA Dec 19 '15 at 18:21
  • @Karol And *that* is the issue with this question -- the absence of specific requirements. – Ondrej Tucny Dec 20 '15 at 00:07
0

You can use Roslyn. Here is an example of how to compile and run your own code in runtime.

BWA
  • 5,672
  • 7
  • 34
  • 45
0

Disclaimer: I'm the owner of the project Eval Expression.NET

This library is very easy to use and allow to evaluate and compile almost all the C# language.

// For single evaluation
var value1 = Eval.Execute<bool>("b > a", new { a = 1, b = 2 });

// For many evaluation
var compiled = Eval.Compile<Func<int, int, bool>>("b > a", "a", "b");
var value2 = compiled(1, 2);
Jonathan Magnan
  • 10,874
  • 2
  • 38
  • 60