8: c#Expression expression directory tree

Preface

What is an expression tree?

1.表达式目录树Expression:System.Linq.Expressions; 
2.描述了多个变量或者和常量之间的关系,按照一定的规则进行组装!我们需要查看的时候还能够展开!
3.运用表达式目录树,可以对我们的SQL语句拼装进行简化。
//lambda匿名方法
Func<int, int, int> func = new Func<int, int, int>((m, n) => m * n + 2);
//目录树声明,右侧可直接编写lambda
Expression<Func<int, int, int>> exp = (m, n) => m * n + 2;
//把如下语句拼装成sql
Expression<Func<People, bool>> lambda = x => x.Age > 5 && x.Id > 5;

How to parse the expression tree?

The following lambda expressions will be parsed layer by layer in the directory tree.

  1. First disassemble it into the left tree binary expression m*n+2. Right tree constant 3.
  2. Then split the left tree m*n. Right tree constant 2.
  3. Then disassemble m*n into the following lambda expressions for the left tree variable m and the right tree variable n
    Insert picture description here
    , which are still parsed layer by layer.
    Just look at the picture.
    Insert picture description here

Expression tree characteristics

1. The lambd expression can be used for quick declaration like a delegate;
2. There can be no statement body, and the declaration can only have one line of code;
3. It can be compiled into a delegate by Compile();

Primary stage: when querying the database, all Sql statements are assembled;
now: LinqToSql;

Practical case of expression catalog number

1. Serialization;
2. Reflection;
3. Expression directory tree;

Code analysis.

这次代码涉及到一些封装的类了。可能看的不是很明朗了。可留言

lambda和expression

{
    
    
    Func<int, int, int> func = (m, n) =>
    {
    
    
        int i = 0;
        return m * n + 2;
    };  //委托  拉姆达表达式其实是作为委托的一个参数,本质是一个方法(匿名方法)
    Expression<Func<int, int, int>> exp = (m, n) => m * n + 2; //数据结构--就像对一个计算做了一个精确的描述,展开之后发现,分为左边,右边,每个元素都可以把值都获取出来,二叉树
    var erpPlu = exp.Compile();//表达式目录树可以通过compile 转换成一个委托
    //Expression<Func<int, int, int>> exp1 = (m, n) => //表达式目录树只能有一行;
    //    {
    
    
    //        return m * n + 2;
    //    };
    //表达式目录树:语法树,或者说是一种数据结构
    int iResult1 = func.Invoke(12, 23);
    int iResult2 = exp.Compile().Invoke(12, 23);
}

Decompile lambda into expression

//手动拼装表达式目录树,不是用的lambda的快捷方式
{
    
    
    //表达式目录树的拼装
    //Expression<Func<int>> expression = () => 123 + 234;  //两个常量相加-----表达式目录树的快捷声明
    //Expression constant123 = Expression.Constant(123);
    //Expression constant234 = Expression.Constant(234);
    //Expression expressionAdd = Expression.Add(constant123, constant234); 
    //var exp = Expression.Lambda<Func<int>>(expressionAdd);
    //var func = exp.Compile();
    //int iResult = func.Invoke(); 
}
{
    
    
//快捷声明--其实编译器提供的便捷功能---语法糖
Expression<Func<int, int, int>> exp = (m, n) => m * n + m + n + 2; 
//表达式太长的话,难写,但是我们可以利用反编译工具,查看编译后的语句;
ParameterExpression parameterExpression = Expression.Parameter(typeof(int), "m");
ParameterExpression parameterExpression2 = Expression.Parameter(typeof(int), "n");
Expression expContant2 = Expression.Constant(2, typeof(int));

Expression multipley = Expression.Multiply(parameterExpression, parameterExpression2);
Expression expAdd = Expression.Add(multipley, parameterExpression);

Expression expAdd1 = Expression.Add(expAdd, parameterExpression2);
Expression expAdd2 = Expression.Add(expAdd1, expContant2);
Expression<Func<int, int, int>> expression = Expression.Lambda<Func<int, int, int>>(expAdd2, new ParameterExpression[]
{
    
    
    parameterExpression,
    parameterExpression2
});
Func<int, int, int> fun = expression.Compile();
int iResult = fun.Invoke(10, 11);
}

end

This part involves a lot of content and coding, so you still need to check tutorials or videos separately for learning. Because it involves a lot of packaging content.

Guess you like

Origin blog.csdn.net/hello_mr_anan/article/details/109270674