1

Given a dynamic if statement in a String is there a method in .net to evaluate and return the correct result.

I.E.

    static void Main(string[] args)
    {
        String a = "if(\"Alex\" == \"Alex\"){return 1;}else{return 0;}";
        passThroughTest.Program.evalStatement(a);
    }
    public static void evalStatement(String statement)
    {

        //Evaluation of Statement

     }

This isn't perfect but in this case the end result would be a 1. This seems 'stupid' from this point of view, but its for a more complex engine which needs to draw on similar syntax to evaluate.

Am I at a point where I need to write a parser of some sort and evaluate the statements...?

Thanks for your help!

Alex Kremer
  • 128
  • 1
  • 7
  • How complex are your statements going to be? http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments is a text book example of using the CodeDom – dash Oct 19 '12 at 23:35
  • Thank you for that link. Im going to dig into it a little more. It should not be too complex; though it could get to a point where its nesting ifs or using switch statements. – Alex Kremer Oct 19 '12 at 23:56
  • This is best and awesome method CLICK [Here](http://stackoverflow.com/questions/1437964/best-and-shortest-way-to-evaluate-mathematical-expressions/1438829#1438829) – Sarbanjeet Mar 26 '14 at 12:14

3 Answers3

3

What I think you are looking for is the CSharpCodeProvider : http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx

This allows you to compile or evaluate C# code from a source and run it. It might not be as easy as evaluating a single "if" case, as when you compile C#, you are expected to have the whole syntax coming with it.

LightStriker
  • 19,738
  • 3
  • 23
  • 27
  • Thank you for the response. Im going to dig into that one and see how it compares to some of the other answers. – Alex Kremer Oct 20 '12 at 00:06
3
  • You can use javascript.net

    Sample code from their website:

    // Initialize a context
    using (JavascriptContext context = new JavascriptContext()) {
    
        // Setting external parameters for the context
        context.SetParameter("console", new SystemConsole());
        context.SetParameter("message", "Hello World !");
        context.SetParameter("number", 1);
    
        // Script
        string script = @"
            var i;
            for (i = 0; i < 5; i++)
                console.Print(message + ' (' + i + ')');
            number += i;
        ";
    
        // Running the script
        context.Run(script);
    
        // Getting a parameter
        Console.WriteLine("number: " + context.GetParameter("number"));
    }
    
  • Or use C# expression tree, see Building Expression Evaluator with Expression Trees

Oliver Bock
  • 4,829
  • 5
  • 38
  • 62
AZ.
  • 7,333
  • 6
  • 44
  • 62
  • 1
    This method seems like it would use less overhead in the long run... Im going to try it out and see how it works. Thanks! – Alex Kremer Oct 20 '12 at 00:04
0

You can also do something like this:

var expression="(\"Alex\" == \"Alex\") ? 1:0"; 
var res = FlexRule.DynamicEvaluation.ExpressionEval.Default.Compute(null, expression);

For more details information of expression please have a look at http://wiki.flexrule.com/index.php?title=Expression

Arash Aghlara
  • 330
  • 3
  • 11