上一篇咱們對錶達式樹有了初步的認識,這裏咱們將對錶達式樹進行遍歷,只有弄清楚了他的運行原理,咱們才能夠對他進行定製化修改。html
表達式系列目錄node
C# 表達式樹講解(一)express
C# 表達式樹遍歷(二)api
C# 表達式樹分頁擴展(三)數據結構
要查看錶達式樹的遍歷,確定不能直接用.Net Framework封裝的方法,由於.Net Framework框架是閉源的,除了看中間語言(IL)去查看。咱們就用ExpressionVisitor類查看一下他的運行原理,看了下ExpressionVisitor類,裏面都是對各個表達式的訪問,並且都是虛擬函數,咱們能夠對他進行override。框架
ExpressionVisitor類裏面都是對各個類型的表達式進行訪問,爲了更好的理解裏面的訪問順序,蝸牛把裏面的虛函數都override了一遍,而後跟蹤裏面的執行順序。【傻眼了,35個虛函數須要override,心裏是很拒絕的,vs2019有沒有重寫父類虛函數的快捷鍵啊!!!!!!!】ide
ExpressionVisitor類相關介紹:https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.expressions.expressionvisitor?view=netframework-4.8函數
爲了避免改變ExpressionVisitor類原來的訪問,建立的SnailExpressionVisitor.cs 文件只在重寫方法裏面添加日誌打印。oop
代碼以下:
public class SnailExpressionVisitor : ExpressionVisitor { public override Expression Visit(Expression node) { Console.WriteLine($"訪問了 Visit,內容:{node.ToString()}"); return base.Visit(node); } protected override CatchBlock VisitCatchBlock(CatchBlock node) { Console.WriteLine($"訪問了 VisitCatchBlock,內容:{node.ToString()}"); return base.VisitCatchBlock(node); } protected override ElementInit VisitElementInit(ElementInit node) { Console.WriteLine($"訪問了 VisitElementInit,內容:{node.ToString()}"); return base.VisitElementInit(node); } protected override LabelTarget VisitLabelTarget(LabelTarget node) { Console.WriteLine($"訪問了 VisitLabelTarget,內容:{node.ToString()}"); return base.VisitLabelTarget(node); } protected override MemberAssignment VisitMemberAssignment(MemberAssignment node) { Console.WriteLine($"訪問了 VisitMemberAssignment,內容:{node.ToString()}"); return base.VisitMemberAssignment(node); } protected override MemberBinding VisitMemberBinding(MemberBinding node) { Console.WriteLine($"訪問了 VisitMemberBinding,內容:{node.ToString()}"); return base.VisitMemberBinding(node); } protected override MemberListBinding VisitMemberListBinding(MemberListBinding node) { Console.WriteLine($"訪問了 VisitMemberListBinding,內容:{node.ToString()}"); return base.VisitMemberListBinding(node); } protected override MemberMemberBinding VisitMemberMemberBinding(MemberMemberBinding node) { Console.WriteLine($"訪問了 VisitMemberMemberBinding,內容:{node.ToString()}"); return base.VisitMemberMemberBinding(node); } protected override SwitchCase VisitSwitchCase(SwitchCase node) { Console.WriteLine($"訪問了 VisitSwitchCase,內容:{node.ToString()}"); return base.VisitSwitchCase(node); } protected override Expression VisitBinary(BinaryExpression node) { Console.WriteLine($"訪問了 VisitBinary,內容:{node.ToString()}"); return base.VisitBinary(node); } protected override Expression VisitBlock(BlockExpression node) { Console.WriteLine($"訪問了 VisitBlock,內容:{node.ToString()}"); return base.VisitBlock(node); } protected override Expression VisitConditional(ConditionalExpression node) { Console.WriteLine($"訪問了 VisitConditional,內容:{node.ToString()}"); return base.VisitConditional(node); } protected override Expression VisitConstant(ConstantExpression node) { Console.WriteLine($"訪問了 VisitConstant,內容:{node.ToString()}"); return base.VisitConstant(node); } protected override Expression VisitDebugInfo(DebugInfoExpression node) { Console.WriteLine($"訪問了 VisitDebugInfo,內容:{node.ToString()}"); return base.VisitDebugInfo(node); } protected override Expression VisitDefault(DefaultExpression node) { Console.WriteLine($"訪問了 VisitDefault,內容:{node.ToString()}"); return base.VisitDefault(node); } protected override Expression VisitDynamic(DynamicExpression node) { Console.WriteLine($"訪問了 VisitDynamic,內容:{node.ToString()}"); return base.VisitDynamic(node); } protected override Expression VisitExtension(Expression node) { Console.WriteLine($"訪問了 VisitExtension,內容:{node.ToString()}"); return base.VisitExtension(node); } protected override Expression VisitGoto(GotoExpression node) { Console.WriteLine($"訪問了 VisitGoto,內容:{node.ToString()}"); return base.VisitGoto(node); } protected override Expression VisitIndex(IndexExpression node) { Console.WriteLine($"訪問了 VisitIndex,內容:{node.ToString()}"); return base.VisitIndex(node); } protected override Expression VisitInvocation(InvocationExpression node) { Console.WriteLine($"訪問了 VisitInvocation,內容:{node.ToString()}"); return base.VisitInvocation(node); } protected override Expression VisitLabel(LabelExpression node) { Console.WriteLine($"訪問了 VisitLabel,內容:{node.ToString()}"); return base.VisitLabel(node); } protected override Expression VisitLambda<T>(Expression<T> node) { Console.WriteLine($"訪問了 VisitLambda,內容:{node.ToString()}"); return base.VisitLambda(node); } protected override Expression VisitListInit(ListInitExpression node) { Console.WriteLine($"訪問了 VisitListInit,內容:{node.ToString()}"); return base.VisitListInit(node); } protected override Expression VisitLoop(LoopExpression node) { Console.WriteLine($"訪問了 VisitLoop,內容:{node.ToString()}"); return base.VisitLoop(node); } protected override Expression VisitMember(MemberExpression node) { Console.WriteLine($"訪問了 VisitMember,內容:{node.ToString()}"); return base.VisitMember(node); } protected override Expression VisitMemberInit(MemberInitExpression node) { Console.WriteLine($"訪問了 VisitMemberInit,內容:{node.ToString()}"); return base.VisitMemberInit(node); } protected override Expression VisitMethodCall(MethodCallExpression node) { Console.WriteLine($"訪問了 VisitMethodCall,內容:{node.ToString()}"); return base.VisitMethodCall(node); } protected override Expression VisitNew(NewExpression node) { Console.WriteLine($"訪問了 VisitNew,內容:{node.ToString()}"); return base.VisitNew(node); } protected override Expression VisitNewArray(NewArrayExpression node) { Console.WriteLine($"訪問了 VisitNewArray,內容:{node.ToString()}"); return base.VisitNewArray(node); } protected override Expression VisitParameter(ParameterExpression node) { Console.WriteLine($"訪問了 VisitParameter,內容:{node.ToString()}"); return base.VisitParameter(node); } protected override Expression VisitRuntimeVariables(RuntimeVariablesExpression node) { Console.WriteLine($"訪問了 VisitRuntimeVariables,內容:{node.ToString()}"); return base.VisitRuntimeVariables(node); } protected override Expression VisitSwitch(SwitchExpression node) { Console.WriteLine($"訪問了 VisitSwitch,內容:{node.ToString()}"); return base.VisitSwitch(node); } protected override Expression VisitTry(TryExpression node) { Console.WriteLine($"訪問了 VisitTry,內容:{node.ToString()}"); return base.VisitTry(node); } protected override Expression VisitTypeBinary(TypeBinaryExpression node) { Console.WriteLine($"訪問了 VisitTypeBinary,內容:{node.ToString()}"); return base.VisitTypeBinary(node); } protected override Expression VisitUnary(UnaryExpression node) { Console.WriteLine($"訪問了 VisitUnary,內容:{node.ToString()}"); return base.VisitUnary(node); } }
調用方法:
Expression<Func<int, int, bool>> fun = (x, y) => x - y > 5; var treeModifier = new SnailExpressionVisitor(); Expression modifiedExpr = treeModifier.Visit(fun);
運行結果:
從打印的日誌裏面能夠看出,
一、每次訪問表達式類時,都會先去調用Visit函數,估計他是在Visit裏面斷定表達式類,而後在根據表達式類的類型,調用訪問改表達式的函數
二、對Lambda表達式類,是先訪問的是Expression<T>。Expression<T>是否是很熟悉,上一章說過他的做用是將強類型Lambda表達式表示爲表達式樹形式的數據結構,解析成功以後纔對表達式的訪問
三、對於表達式先解析的是左邊,左邊的內容解析完了以後在解析右邊,如(x-y)>5,解析的順序是:x-y=>x=>y=>5
既然咱們弄清楚了表達式樹的訪問,如今咱們就能夠對他進行編輯修改了。
上面咱們判斷的是x-y>5,如今咱們規定,將「-」改爲「+」,「>」改爲「>=」
對VisitBinary方法修改代碼以下:
protected override Expression VisitBinary(BinaryExpression node) { Console.WriteLine($"訪問了 VisitBinary,內容:{node.ToString()}"); if (node.NodeType == ExpressionType.GreaterThan) { Expression left = this.Visit(node.Left); Expression right = this.Visit(node.Right); var result = Expression.MakeBinary(ExpressionType.GreaterThanOrEqual, left, right, node.IsLiftedToNull, node.Method); Console.WriteLine($"訪問了 VisitBinary,更改以後的內容:{result.ToString()}"); return result; } else if (node.NodeType == ExpressionType.Subtract || node.NodeType == ExpressionType.SubtractChecked) { Expression left = this.Visit(node.Left); Expression right = this.Visit(node.Right); var result = Expression.MakeBinary(ExpressionType.Add, left, right, node.IsLiftedToNull, node.Method); Console.WriteLine($"訪問了 VisitBinary,更改以後的內容:{result.ToString()}"); return result; } else { return base.VisitBinary(node); } }
調用方法:
Expression<Func<int, int, bool>> fun = (x, y) => x - y > 5; var treeModifier = new SnailExpressionVisitor(); Expression modifiedExpr = treeModifier.Visit(fun); Console.WriteLine($"Lambda的轉換最後結果:{modifiedExpr.ToString()}");
運行結果以下
對錶達樹的講解已經完成了,可是說了這麼久,對真實的開發有什麼做用呢?後面我將利用Lambda表達式寫一個對現有數據分頁的公共方法,同時在對Dapper的擴展也會用到相關知識點,你們拭目以待吧……