Overview
In. NET Framework 3.5, with the support of LINQ, has won the favor of developers with its powerful and elegant programming methods, and various LINQ providers are even more powerful, such as LINQ to nhib.pdf and LINQ to Google. As well as good scalability, we can easily write our own LINQ Provider.
This article is the first article in the series of articles about creating your own LINQ Provider. It mainly introduces the knowledge of Expression Tree.
Recognize the expression directory tree
What is the Expression Tree? It is an abstract syntax Tree or a data structure. By parsing the Expression Tree, some of our specific functions can be implemented (we will talk about it later). First, let's look at how to construct an expression directory tree. The simplest way is to use Lambda expressions. Let's look at the following code:
Expression<Func<int, int, int>> expression = (a, b) => a * b + 2;
When we specify a Lambda Expression to a variable (parameter) of the Expression <TDelegate> type, the compiler will issue a command to generate the Expression directory tree, as shown in the Lambda Expression (, b) => a * B + 2 creates an expression directory tree, which represents a data structure, that is, we represent a line of code in the form of a data structure, specifically, the final constructed expression directory tree shape is shown in:
Each node represents an expression, which may be a binary operation or a constant or a parameter. For example, ParameterExpression is a parameter expression and ConstantExpression is a constant expression, binaryExpression is a binary expression. You can also use Expression Tree Visualizer in Visual Studio to view the Expression directory Tree:
Shows the result:
Expression Tree Visualizer can be obtained from the LINQ Sample on the MSDN Code Gallery. Now we know the composition of the expression directory tree to see which expressions are provided by. NET Framework? As shown in:
They all inherit from abstract base class expressions, while generic expressions <TDelegate> inherit from LambdaExpression. A large number of factory methods are provided in the Expression class. These methods are used to create the above Expression objects, such as calling Add () the method will create a BinaryExpression object that represents the arithmetic addition operation without Overflow check. Calling the Lambda Method will create a LambdaExpression object that represents the lambda expression. For details, refer to MSDN. The Lambda expression is used to construct the expression directory tree. Now we can see how to manually construct an expression directory tree using these expression objects, as shown in the following code:
static void Main(string[] args){ ParameterExpression paraLeft = Expression.Parameter(typeof(int), "a"); ParameterExpression paraRight = Expression.Parameter(typeof(int), "b"); BinaryExpression binaryLeft = Expression.Multiply(paraLeft, paraRight); ConstantExpression conRight = Expression.Constant(2, typeof(int)); BinaryExpression binaryBody = Expression.Add(binaryLeft, conRight); LambdaExpression lambda = Expression.Lambda<Func<int, int, int>>(binaryBody, paraLeft, paraRight); Console.WriteLine(lambda.ToString()); Console.Read();}
The constructed expression directory tree is still shown in:
Run this code to see what is output:
We can see that through manual construction, we have indeed constructed the same Lambda expression as above. For an expression directory tree, it has several important attributes:
Body: the Body of the expression;
Parameters: the parameter of the expression;
NodeType: indicates the node type of the expression. In the above example, its node type is Lambda;
Type: the static Type of the expression. In the preceding example, Type is Fun <int, int, int>.
In Expression Tree Visualizer, we can see the related attributes of the Expression directory Tree, as shown in:
Expression directory tree and delegate
You may often see the following languages. The first sentence is to initialize the Func delegate using a Lambda expression, and the second sentence uses a Lambda expression to construct an expression directory tree, what is the difference between them?
static void Main(string[] args){ Func<int, int, int> lambda = (a, b) => a + b * 2; Expression<Func<int, int, int>> expression = (a, b) => a + b * 2;}
In fact, let's take a look at IL. The first sentence directly compiles the Lambda expression into IL, as shown in the following code:
.method private hidebysig static void Main(string[] args) cil managed{ .entrypoint .maxstack 3 .locals init ([0] class [System.Core]System.Func`3<int32,int32,int32> lambda) IL_0000: nop IL_0001: ldsfld class [System.Core]System.Func`3<int32,int32,int32> TerryLee.LinqToLiveSearch.Program::'CS$<>9__CachedAnonymousMethodDelegate1' IL_0006: brtrue.s IL_001b IL_0008: ldnull IL_0009: ldftn int32 TerryLee.LinqToLiveSearch.Program::'<Main>b__0'(int32, int32) IL_000f: newobj instance void class [System.Core]System.Func`3<int32,int32,int32>::.ctor(object, native int) IL_0014: stsfld class [System.Core]System.Func`3<int32,int32,int32> TerryLee.LinqToLiveSearch.Program::'CS$<>9__CachedAnonymousMethodDelegate1' IL_0019: br.s IL_001b IL_001b: ldsfld class [System.Core]System.Func`3<int32,int32,int32> TerryLee.LinqToLiveSearch.Program::'CS$<>9__CachedAnonymousMethodDelegate1' IL_0020: stloc.0 IL_0021: ret}
The second sentence tells the compiler that it is an expression directory tree. Therefore, the compiler analyzes the Lambda expression and generates an expression directory tree that represents the Lambda expression, that is, it is consistent with the IL generated by the directory tree that we created manually. The following code is shown. Some code is omitted to save space:
.method private hidebysig static void Main(string[] args) cil managed{ .entrypoint .maxstack 4 .locals init ([0] class [System.Core]System.Linq.Expressions.Expression`1< class [System.Core]System.Func`3<int32,int32,int32>> expression, [1] class [System.Core]System.Linq.Expressions.ParameterExpression CS$0$0000, [2] class [System.Core]System.Linq.Expressions.ParameterExpression CS$0$0001, [3] class [System.Core]System.Linq.Expressions.ParameterExpression[] CS$0$0002) IL_0000: nop IL_0001: ldtoken [mscorlib]System.Int32 IL_0006: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(...) IL_000b: ldstr "a" IL_0010: call class [System.Core]System.Linq.Expressions.ParameterExpression [System.Core]System.Linq.Expressions.Expression::Parameter( class [mscorlib]System.Type, IL_0038: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle() IL_003d: call class [System.Core]System.Linq.Expressions.ConstantExpression [System.Core]System.Linq.Expressions.Expression::Constant(object, class [mscorlib]System.Type) IL_0042: call class [System.Core]System.Linq.Expressions.BinaryExpression [System.Core]System.Linq.Expressions.Expression::Multiply(class [System.Core]System.Linq.Expressions.Expression, class [System.Core]System.Linq.Expressions.Expression) IL_0047: call class [System.Core]System.Linq.Expressions.BinaryExpression [System.Core]System.Linq.Expressions.Expression::Add(class [System.Core]System.Linq.Expressions.Expression, class [System.Core]System.Linq.Expressions.Expression) IL_004c: ldc.i4.2 IL_004d: newarr [System.Core]System.Linq.Expressions.ParameterExpression}
I believe everyone can understand the differences between them here to deepen the differences between the expression directory tree.
Execution expression directory tree
We can create an expression directory tree. Now let's see how to execute the expression directory tree. We need to call the Compile method to create an executable delegate and call the delegate, as shown in the following code:
static void Main(string[] args){ ParameterExpression paraLeft = Expression.Parameter(typeof(int), "a"); ParameterExpression paraRight = Expression.Parameter(typeof(int), "b"); BinaryExpression binaryLeft = Expression.Multiply(paraLeft, paraRight); ConstantExpression conRight = Expression.Constant(2, typeof(int)); BinaryExpression binaryBody = Expression.Add(binaryLeft, conRight); Expression<Func<int, int, int>> lambda = Expression.Lambda<Func<int, int, int>>(binaryBody, paraLeft, paraRight); Func<int, int, int> myLambda = lambda.Compile(); int result = myLambda(2, 3); Console.WriteLine("result:" + result.ToString()); Console.Read();}
Output result after running:
Here, we only need to simply call the Compile method. In fact. NET Framework calls an internal class named ExpressionCompiler to execute the expression directory tree (note that Compiler here is not the same as Compiler compilation ). In addition, you can only execute the Expression directory tree that represents the Lambda Expression, that is, the LambdaExpression or Expression <TDelegate> type. If the expression directory tree does not represent a Lambda expression, you must call the Lambda Method to create a new expression. The following code:
static void Main(string[] args){ BinaryExpression body = Expression.Add( Expression.Constant(2), Expression.Constant(3)); Expression<Func<int>> expression = Expression.Lambda<Func<int>>(body, null); Func<int> lambda = expression.Compile(); Console.WriteLine(lambda());}
Access and modify the expression directory tree
As I mentioned at the beginning of this article, by parsing the expression directory tree, we can implement some specific functions. to parse the expression directory tree, access to the expression directory tree is naturally inevitable. In. NET Framework, an abstract expression ExpressionVisitor class is provided in the directory tree, but it is an internal and cannot be accessed directly. Fortunately, Microsoft provides the ExpressionVisitor class implementation in MSDN, which can be used directly. This class is an abstract class. Microsoft aims to implement its own expression directory tree metadata class based on the integration of ExpressionVisitor. Now let's look at the simple expression directory tree:
static void Main(string[] args){ Expression<Func<int, int, int>> lambda = (a, b) => a + b * 2; Console.WriteLine(lambda.ToString());}
Output:
Now we want to modify the expression directory tree so that the Lambda expression it represents is (a, B) => (a-(B * 2 )), in this case, you need to write your own expression directory tree accesser, as shown in the following code:
public class OperationsVisitor : ExpressionVisitor{ public Expression Modify(Expression expression) { return Visit(expression); } protected override Expression VisitBinary(BinaryExpression b) { if (b.NodeType == ExpressionType.Add) { Expression left = this.Visit(b.Left); Expression right = this.Visit(b.Right); return Expression.Subtract(left,right); } return base.VisitBinary(b); }}
Use the expression directory tree accessor to modify the expression directory tree, as shown in the following code:
static void Main(string[] args){ Expression<Func<int, int, int>> lambda = (a, b) => a + b * 2; var operationsVisitor = new OperationsVisitor(); Expression modifyExpression = operationsVisitor.Modify(lambda); Console.WriteLine(modifyExpression.ToString());}
After running, you can see the output:
It seems that we are modifying the expression directory tree, but we are not all right. We just modify a copy of the expression directory tree, because the expression directory tree is immutable, we cannot directly modify the expression directory tree. You can see the implementation of the OperationsVisitor class above. During the modification, we copied the node of the expression directory tree.
Why expression directory tree?
Through the previous introduction, I believe you have some knowledge about the expression directory tree. Another important question is why the expression directory tree is required? At the beginning of this article, we mentioned that by parsing the expression directory tree, we can implement some of our specific functions. Take the LINQ to SQL as an example. Let's look at the figure below:
When we compile a query Expression in C #, it returns a value of the IQueryable type, which contains two important attributes: Expression and Provider, the following code:
The query expression we compiled will be encapsulated into an abstract data structure, which is the expression directory tree. When we use the value returned above, the compiler will translate the value in the desired way, which is determined by Expression and Provider. As you can see, this will be very flexible and highly scalable. With the expression directory tree, you can freely compile your own Provider to query the desired data source. It is often said that LINQ provides a unified programming method for accessing different data sources, and its mysteries are here. However, it is important to note that no specific LINQ Provider is required for LINQ to Objects, because it is not translated as an expression directory tree, which will be mentioned later.
Summary
This topic describes the expression directory tree in detail and lays a foundation for writing our own LINQ Provider. I hope it will be helpful to you. To view the various types of lINQ providers on the Internet, visit all kinds of LINQ.
Address: http://www.cnblogs.com/Terrylee/archive/2008/08/01/custom-linq-provider-part-1-expression-tree.html