You cannot create dynamic statements, but you can wrap the statement in a delegate. You can use lambdas to simplify the syntax.
Func<int,int,int> opPlus = (a,b) => a + b; Func<int,int,int> opMinus = (a,b) => a - b; // etc.. // now you can write: int a = 5, b = 6; Func<int,int,int> op = opPlus; if( op(a,b) > 9 ) DoSomething();
Although this is not certain - the future direction for C # is to implement the compiler as a service. Thus, at some point it may be possible to write code that dynamically evaluates the expression.
source share