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")); */ } } }

  

posted @ 2021-08-22 14:31  艾特-天空之海  阅读(31)  评论(0编辑  收藏  举报