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

5
  • C# eval equivalent? Commented Aug 11, 2009 at 12:31
  • stackoverflow.com/questions/4629/c-eval-equivalent Commented Aug 11, 2009 at 12:31
  • Can you include to project MSScript.dll, if so I would provide an answer Commented Aug 11, 2009 at 12:32
  • a = "<<x>>/100"; a.replace("<<x>>","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); Commented Aug 11, 2009 at 13:13
  • Possible duplicate of How can I evaluate C# code dynamically? Commented Jul 9, 2018 at 14:26

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This is very practical solution for quick evaluations in any .NET language. You just need to include a reference to Microsoft.JScript.dll also.
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.)

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.