1

Possible Duplicate:
C# eval equivalent?

Duplicate of How can I evaluate C# code dynamically?

How can we Implement JS eval() in C# If possible provide an example.. thank you

Community
  • 1
  • 1
Preetham
  • 101
  • 3
  • 10
  • Can you include to project MSScript.dll, if so I would provide an answer – Dewfy Aug 11 '09 at 12:32
  • a = "<>/100"; a.replace("<>","20"); string b = eval(a); i should get .2 as result how can i do it in C# without using JScript is it possible? to replace eval(a); – Preetham Aug 11 '09 at 13:13
  • [C# eval equivalent?](http://stackoverflow.com/questions/4629/c-eval-equivalent) – rahul Aug 11 '09 at 12:31
  • http://stackoverflow.com/questions/4629/c-eval-equivalent – Mr. Smith Aug 11 '09 at 12:31
  • Possible duplicate of [How can I evaluate C# code dynamically?](https://stackoverflow.com/questions/4629/how-can-i-evaluate-c-sharp-code-dynamically) – Cœur Jul 09 '18 at 14:26

3 Answers3

4

You can actually use the JScript eval function from C#...

Create a file JsMath.js, with the following JScript code :

class JsMath
{
    static function Eval(expression : String) : double
    {
        return eval(expression);
    };
}

Compile it into a DLL :

jsc /t:library JsMath.js

Add a reference to JsMath.dll to your project. You can now use the JsMath class in your code :

double result = JsMath.Eval(expression);
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • This is very practical solution for quick evaluations in any .NET language. You just need to include a reference to Microsoft.JScript.dll also. – gavi Jul 18 '13 at 17:35
0

If you can use C# 3.0 / .NET 3.5, there's a sample of a fully operational expression parser in C# Samples for Visual Studio 2008 at MSDN Code Gallery, under DynamicQuery. This makes it possible not just to evaluate single expressions, but even to create functions (delegates) from them, or LINQ expressions. (The LinqDataSource control uses a slightly modified version of this sample internally.)

Ruben
  • 15,217
  • 2
  • 35
  • 45
-1

Go to http://json.org/ and scroll to the bottom. Look for C# in the left column.

geowa4
  • 40,390
  • 17
  • 88
  • 107