What is C#. NET equivalent of Javascript's eval() ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Elliot
    New Member
    • Sep 2010
    • 7

    What is C#. NET equivalent of Javascript's eval() ?

    I have a programs with thousands of variables. I need to perform complex operations on each of these variables that involves other variables.

    What I would like to do is create a two column spreadsheet containing each of the variable names and the formula (stored as a string) used to calculate that variable's value. For each variable in the C# program, I would look up the string containing the formula and then calculate that formula.

    Simple example:
    Let's assume that there are three variables: x, y, and z.

    x = 10
    y = 20

    I look up the formula for z and it returns the following string:

    string = "x + y"

    I am looking for a function that would do the equivalent of:

    z = eval(string)
    with the result that z would equal 30.

    How can I do this in C#?

    Many thanks in advance for any help.
  • Elliot
    New Member
    • Sep 2010
    • 7

    #2
    One further clarification:

    Some of the formulas that I will using will call methods that I have defined.

    So the string for z may be "x + y", but it might also be "NewMethod(x,y) "

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      If it can be done, it will probably involve System.Reflecti on

      Comment

      • Elliot
        New Member
        • Sep 2010
        • 7

        #4
        Thanks for replying to my question. I looked up System.Reflecti on but have no idea how to implement it to solve this problem. Do you have a suggestion of how this System.Reflecti on might help in this case?

        Comment

        • Elliot
          New Member
          • Sep 2010
          • 7

          #5
          Perhaps it would be helpful if I were to try to clarify what I'm looking to do:

          Let's assume that we have a program with the following variables:

          double x1 = 20;
          double x2 = 30;
          double y = 100;
          string s = "y + NewMethod(x1, x2)";


          Furthermore, let's assume that NewMethod() is some method that performs a calculation on its two parameters and returns a value of type double. For example, let's assume that NewMethod(20,30 ) would produce 610.23 as a result.

          I am looking for a method (let's call it "Formula()" for demonstation purposes), such that:

          z = Formula(s);

          would produce the same result as:

          z = y + NewMethod(x1, x2);

          (i.e. z would be equal to 710.23)

          Comment

          • Alex Papadimoulis
            Recognized Expert New Member
            • Jul 2010
            • 26

            #6
            There is no "easy" way to do this in C#.

            C# (and other .NET languages) are compiled into MSIL, and the Common Language Runtime (which executes MSIL) has no understanding of C#, VB, or any other .NET language. Interpreted languages (like JavaScript) have runtime engines that can understand and execute code.

            That said, there are a few ways to handle this:

            Write a parser/compiler for your specific needs - there are several tools (including the great GOLD Parsing System that can do all of the heavy lifting.

            Use Windows Scripting Host - this is obviously Windows-specific, but it will enable you to execute Java or VB script and pass in your own objects. See msdn for more info.

            Compile at run-time - this can get very tricky; there are plenty of helper classes within Reflection.Emit that can help you out.

            Comment

            Working...