using System;
using System.Linq;
using System.Linq.Expressions;
class Program {
static void Main(string[] args) {
Expression<Func<int, bool>> isOddExpression = i => (i & 1) == 1;
ParameterExpression param = Expression.Parameter(typeof(int), "i");
Expression<Func<int, bool>> isOdd =
Expression.Lambda<Func<int, bool>>(
Expression.Equal(
Expression.And(
param,
Expression.Constant(1, typeof(int))),
Expression.Constant(1, typeof(int))),
new ParameterExpression[] { param });
Func<int, bool> isOddCompiledExpression = isOddExpression.Compile();
for (int i = 0; i < 10; i++) {
if (isOddCompiledExpression(i))
Console.WriteLine(i + " is odd");
else
Console.WriteLine(i + " is even");
}
}
}
|