Expression小案例
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace _Console { public static class ExpressionTest { //Expression<Func<string, bool>> f = s => s.Length < 5; public static void Test() { ParameterExpression p = Expression.Parameter(typeof(string), "s"); MemberExpression stringLength = Expression.Property(p, "Length"); ConstantExpression five = Expression.Constant(5); BinaryExpression comparison = Expression.LessThan(stringLength, five); Expression<Func<string, bool>> lambda = Expression.Lambda<Func<string, bool>>(comparison, p); //end /* 将lambda编译为委托 Func<string, bool> runnable = lambda.Compile(); Console.WriteLine(runnable("kangaroo")); Console.WriteLine(runnable("dog")); */ } } }