C # 3.0's new support frame 10 expression tree 03 types of expression trees

The presence of large lists .NET Core framework expression tree may be used in conjunction with the class. Can  System.Linq.Expressions  see the full list. Let's take a look at the kind of framework design approach, rather than one by one to see the full list.

Design language, expressions are calculated and body return code value. Expression may be very simple: constant expression  1 returns a constant value. They may be more complex: expression  (-B + Math.Sqrt(B*B - 4 * A * C)) / (2 * A) returns the root of a quadratic equation (if the equation has a solution).

System.Linq.Expression

Use of the expression tree One difficulty is that many different types of expressions are valid number of locations in the program. Consider an assignment expression. The right side of the assignment can be a constant value, variable, method invocation expression or other content. Language flexibility means that, when traversing the expression tree, you may encounter many different types of expression anywhere in the node tree. Therefore, when using the expression type base, the easiest to understand. However, sometimes you need to know more. For this purpose, the base class contains an expression  NodeType properties. It returns  ExpressionType, it is possible expression types of enumeration. Knowing the type of the node, it can be converted to that type, and performs a specific operation (if you know the type of the expression node). You can search for specific node types, and then use the specific attributes of this expression.

For example, this code will print the variable name to access the variable expression. My approach is to first check the node type, then converted to variable access expression, then view a particular expression type of property:

Expression<Func<int, int>> addFive = (num) => num + 5;

            if (addFive.NodeType == ExpressionType.Lambda)
            {
                var lambdaExp = (LambdaExpression)addFive;

                var parameter = lambdaExp.Parameters.First();

                Console.WriteLine("参数名称:" + parameter.Name);
                Console.WriteLine("参数类型:" + parameter.Type);
            }

            Console.ReadKey();
        }

Output:

Create an expression tree

System.Linq.Expression Class also contains a number of static methods to create expressions. These methods use parameters to create a child node provides expression node. In this way, you can build an expression from the leaf node. For example, the code generates an Add expression:

// adder is "1 + 2" plus expression 
var One Expression.Constant = ( 1 , typeof ( int ));
 var TWO = Expression.Constant ( 2 , typeof ( int ));
 var Addition Expression.Add = ( one, two);

From this simple example, you will find that creating and using an expression tree involving many types. The complexity is to provide a rich vocabulary provided by the C # language features necessary.

Navigation API

There is a mapping to the expression node type in almost all syntax elements of the C # language. Each type has a specific method for this kind of language elements. Many remember the one-time content. I'm not going to remember everything, but will adopt tricks for using the expression tree, as follows:

  1. View  ExpressionType enumeration members to determine possible nodes should be checked. If you want to understand and traverse the expression tree, which will be very useful.
  2. Check  Expression static member class to generate expression. These methods can generate any type of expression from its child node set.
  3. View  ExpressionVisitor class to generate a modified expression tree.

Guess you like

Origin www.cnblogs.com/SavionZhang/p/11183396.html