0

I have an expression like "(((2+5)*1000)/((30-20)*7))" How can I evaluate the expression using IronJS from C#. The expected result is 100.

I tried the top two rated answers described in How can I evaluate a C# expression dynamically? and it worked in those ways.Fine.

I have also looked into the solution proposed by @Rudolf_Abel and @Jeno Laszlo mentioned in the comment that IronJS is a better choice.

Now I am trying the same with IronJS. Is it possible.How ? I have read some article on IronJs (e.g. here is one) but just could not fit that into the requirement.

N.B.~ This is for my own sake of interest I am performing this experiment.

Community
  • 1
  • 1
priyanka.sarkar
  • 25,766
  • 43
  • 127
  • 173

2 Answers2

1

Here's a sample from IronJS source, and here's another one.

So, your code will be something like this:

private static void Run()
{
        var ctx = new IronJS.Hosting.CSharp.Context();

        dynamic a = ctx.Execute("var a = (((2+5)*1000)/((30-20)*7)); a;");
        Console.WriteLine(a);
}

P.S. As for me, I'd prefer to use C# scripting API instead. Stable version will be released with VS2015 update1 (coming soon) and nightly builds are in a good condition, I've had no troubles with them.

All you need to start coding: small intro and nuget package. Good luck!:)

Sinix
  • 1,306
  • 9
  • 46
1

I don't know if this gonna help, but please try it out.

IronJS.Hosting.Context jsContext;
jsContext = IronJS.Hosting.Context.Create();
jsContext.Execute("(function(){return eval('(((2+5)*1000)/((30-20)*7))');})()");
choz
  • 17,242
  • 4
  • 53
  • 73