Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a .NET class that represents operator types?

Tags:

c#

.net

I would like to do the following:

*OperatorType* o = *OperatorType*.GreaterThan;

int i = 50;

int increment = -1;

int l = 0;

for(i; i o l; i = i + increment)
{
    //code
}

this concept can be kludged in javascript using an eval()... but this idea is to have a loop that can go forward or backward based on values set at runtime.

is this possible?

Thanks

like image 660
user323774 Avatar asked May 04 '10 17:05

user323774


1 Answers

Yes, it's in .NET Expression trees. Specifically, you need to use BinaryExpression.Add(). Building expression trees doesn't need to be done by hand, the compiler will be happy to convert any lambda expression it sees assigned to Expression<T> into a valid Expression tree.

// Creating an expression tree.
Expression<Func<int, int, bool>> greaterThan = (l, r) => l > r;

int i = 50;

int increment = -1;

int l = 0;

for(i; greaterThan(o, i); i = i + increment)
{
    //code
}

Invoking your expression tree will automatically compile it into a dynamic method and greaterThan will effectively act like a delegate.

like image 59
Johannes Rudolph Avatar answered Sep 20 '22 00:09

Johannes Rudolph