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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With