(精华)2020年8月11日 C#基础知识点 表达式目录树的使用

(精华)2020年8月11日 C#基础知识点 表达式目录树的使用

public static void Show()
        {<!-- -->
            {<!-- -->
                //-----------------------------表达式目录树和委托的比较--------------------------
                Func<int, int, int> func = (m, n) => m * n + 2; //匿名方法
                Expression<Func<int, int, int>> exp = (m, n) => m * n + 2;
                //声明一个表达式目录树
                //快捷方式的声明 数据结构;
                //类似于一个树形结构,描述不同变量和常量之间的关系  数据结构;  
                //表达式目录树:语法树,或者说是一种数据结构
                int iResult1 = func.Invoke(12, 23);
                //exp.Compile()=> 委托; 
                int iResult2 = exp.Compile().Invoke(12, 23); //12 * 23 +2  278

                //int iResult = func.Invoke(12, 13); 
                //Expression<Func<int, int, int>> exp1 = (m, n) =>
                //    {<!-- -->
                //        return m * n + 2;
                //    }; 不能有语句体,不能当做一个方法,不能有大括号,只能有一行代码 
            }
            {<!-- -->
                //----------------------手动拼装表达式目录树,不是用的lambda的快捷方式--------------
                {<!-- -->
                    //Expression<Func<int>> expression = () => 123 + 234; 
                    ConstantExpression left = Expression.Constant(123, typeof(int));
                    ConstantExpression right = Expression.Constant(234, typeof(int));
                    var plus = Expression.Add(left, right);
                    Expression<Func<int>> expression = Expression.Lambda<Func<int>>(plus, new ParameterExpression[0]);
                    int iResult = expression.Compile().Invoke();
                    int iResult1 = expression.Compile()();
                }
                {<!-- -->
                    // 更复杂一点的:
                    //Expression<Func<int, int, int>> expression = (m, n) => m * n + m + n + 2; 
                    ParameterExpression m = Expression.Parameter(typeof(int), "m");
                    ParameterExpression n = Expression.Parameter(typeof(int), "n");
                    ConstantExpression constant2 = Expression.Constant(2, typeof(int));
                    var multiply = Expression.Multiply(m, n);
                    var plus1 = Expression.Add(multiply, m);
                    var plus2 = Expression.Add(plus1, n);
                    var plus = Expression.Add(plus2, constant2);
                    Expression<Func<int, int, int>> expression = Expression.Lambda<Func<int, int, int>>(plus, new ParameterExpression[]
                    {<!-- -->
                        m,
                        n
                    });
                    int iResult = expression.Compile().Invoke(12, 10);

                }
                {<!-- -->
                    //Expression<Func<People, bool>> lambda = (x) => x.Id.ToString().Equals("5"); 
                    ParameterExpression x = Expression.Parameter(typeof(People), "x");
                    FieldInfo field = typeof(People).GetField("Id");
                    ConstantExpression constant5 = Expression.Constant("5");
                    var fieldExp = Expression.Field(x, field);
                    MethodInfo toString = typeof(int).GetMethod("ToString", new Type[] {<!-- --> });
                    MethodInfo equals = typeof(string).GetMethod("Equals", new Type[] {<!-- --> typeof(string) });
                    var tostringExp = Expression.Call(fieldExp, toString, new Expression[0]);
                    var equalsExp = Expression.Call(tostringExp, equals, new Expression[]
                      {<!-- -->
                         constant5
                      });
                    Expression<Func<People, bool>> expression = Expression.Lambda<Func<People, bool>>(equalsExp, new ParameterExpression[]
                    {<!-- -->
                           x
                    });
                    bool bResult = expression.Compile().Invoke(new People()
                    {<!-- -->
                        Id = 5,
                        Name = "德玛西亚"
                    });
                }
            }
            {<!-- -->
                //动态拼装Linq To Sql
                {<!-- -->
                    var DbSet = new List<People>().AsQueryable();
                    //Expression<Func<People, bool>> exp = a => a.Name.Contains("name") && a.NAccount.Contains("Admin"); 
                    Expression<Func<People, bool>> exp = null;
                    Console.WriteLine("用户输入个名称,为空就跳过");
                    string name = Console.ReadLine();
                    if (!string.IsNullOrWhiteSpace(name))
                    {<!-- -->
                        exp = a => a.Name.Contains(name); 
                        DbSet = DbSet.Where(a => a.Name.Contains(name));
                    }
                    Console.WriteLine("用户输入个账号,为空就跳过");
                    string account = Console.ReadLine();
                    if (!string.IsNullOrWhiteSpace(account))
                    {<!-- -->
                        exp = a => a.Account.Contains(account);
                        DbSet = DbSet.Where(a => a.Account.Contains(account));// 出现整个DBSet 暴露出来了,在使用的时候是非常危险
                    }
                    exp = a => a.Account.Contains(account) && a.Name.Contains(name);
                }
            }
        }
posted @ 2020-12-31 09:09  不要摸我的腰  阅读(108)  评论(0编辑  收藏  举报