Yesterday, I posted an answer to a question on Code Project dealing with evaluating expressions in C#. The person who asked the question wanted to know how to evaluate expressions with an eval() function (like the one in JavaScript/JScript).
After playing around with some code snippets, I came up with the following answer:
using Microsoft.JScript; //Microsoft.JScript.dll
using Microsoft.JScript.Vsa; //Microsoft.Vsa.dll
string expr = @"
var x = 11;
var y = 'A';
var bEval = false
if ((x > 10) && (y == 'A'))
{
bEval = true;
}
else
{
bEval = false;
}";
VsaEngine engine = VsaEngine.CreateEngine();
object o = null;
try
{
o = Eval.JScriptEvaluate(expr,engine);
}
catch(Exception e)
{
Console.WriteLine(e.Source);
Console.WriteLine(e.StackTrace);
}
Console.WriteLine(o); //Outputs 'True' to the console
Console.ReadLine();
Enjoy the code!