表達式樹

来源:https://www.cnblogs.com/bibi-feiniaoyuan/archive/2020/03/24/12562362.html
-Advertisement-
Play Games

記錄表達式樹的學習過程 表達式樹將代碼表示為可以檢測、修改、或執行的一種結構,一種定義代碼的結構。 表達式樹是代碼的完整表示形式:可以看到任何子表達式的值。 可以看到方法和屬性名稱。 可以看到任何常數表達式的值。 還可以將自己轉換為可執行的委托,並執行代碼。 通過表達式樹 API,可創建幾乎任何有效 ...


記錄表達式樹的學習過程

表達式樹將代碼表示為可以檢測、修改、或執行的一種結構,一種定義代碼的結構。

表達式樹是代碼的完整表示形式:可以看到任何子表達式的值。 可以看到方法和屬性名稱。 可以看到任何常數表達式的值。 還可以將自己轉換為可執行的委托,並執行代碼。

通過表達式樹 API,可創建幾乎任何有效代碼構造的樹。 但不能在表達式樹中創建某些 C# 習慣用語,第一 非同步的async、await第二是迴圈(迴圈有什麼習慣用語?我凌亂了)。

 表達式樹是不可變的數據結構, 只能換新樹

 

訪問表達式樹變數

 public static void Main()
 {            
     Expression<Func<int, int>> addFive = num => num + 5;
     // ExpressionType有Add、AddChecked、And、AndAlso、Divide、Equal、Lambda、Multiply等。
     if (addFive.NodeType== ExpressionType.Lambda)
     {
         var lambdaExp = (LambdaExpression)addFive;
         var parameter = lambdaExp.Parameters.First();
         Console.WriteLine(parameter.Name);
         Console.WriteLine(parameter.Type);

         Console.WriteLine(lambdaExp.Parameters.Last().Name); //跟上面同一個
         Console.WriteLine(lambdaExp.Parameters.Last().Type);                
     }
 }

創建表達式樹

public static void Main()
{           
    // 常量表達式樹
    ConstantExpression one = Expression.Constant(1, typeof(int));
    ConstantExpression two = Expression.Constant(2, typeof(int));
    // 二進位表達式樹
    BinaryExpression addition = Expression.Add(one, two);
}

 

表達式樹API

ExpressionType:(ConvertChecked 和Convert明明c#寫法一樣,怎麼表達不同節點類型,迷茫... Multiply和MultiplyChecked,迷茫...,NewArrayBounds 多維數組)

//
// 摘要:
//     An addition operation, such as a + b, without overflow checking, for numeric
//     operands.
Add = 0,
//
// 摘要:
//     An addition operation, such as (a + b), with overflow checking, for numeric operands.
AddChecked = 1,
//
// 摘要:
//     A bitwise or logical AND operation, such as (a & b) in C# and (a And b) in Visual
//     Basic.
And = 2,
//
// 摘要:
//     A conditional AND operation that evaluates the second operand only if the first
//     operand evaluates to true. It corresponds to (a && b) in C# and (a AndAlso b)
//     in Visual Basic.
AndAlso = 3,
//
// 摘要:
//     An operation that obtains the length of a one-dimensional array, such as array.Length.
ArrayLength = 4,
//
// 摘要:
//     An indexing operation in a one-dimensional array, such as array[index] in C#
//     or array(index) in Visual Basic.
ArrayIndex = 5,
//
// 摘要:
//     A method call, such as in the obj.sampleMethod() expression.
Call = 6,
//
// 摘要:
//     A node that represents a null coalescing operation, such as (a ?? b) in C# or
//     If(a, b) in Visual Basic.
Coalesce = 7,
//
// 摘要:
//     A conditional operation, such as a > b ? a : b in C# or If(a > b, a, b) in Visual
//     Basic.
Conditional = 8,
//
// 摘要:
//     A constant value.
Constant = 9,
//
// 摘要:
//     A cast or conversion operation, such as (SampleType)obj in C#or CType(obj, SampleType)
//     in Visual Basic. For a numeric conversion, if the converted value is too large
//     for the destination type, no exception is thrown.
Convert = 10,
//
// 摘要:
//     A cast or conversion operation, such as (SampleType)obj in C#or CType(obj, SampleType)
//     in Visual Basic. For a numeric conversion, if the converted value does not fit
//     the destination type, an exception is thrown.
ConvertChecked = 11,
View Code
//
// 摘要:
//     A division operation, such as (a / b), for numeric operands.
Divide = 12,
//
// 摘要:
//     A node that represents an equality comparison, such as (a == b) in C# or (a =
//     b) in Visual Basic.
Equal = 13,
//
// 摘要:
//     A bitwise or logical XOR operation, such as (a ^ b) in C# or (a Xor b) in Visual
//     Basic.
ExclusiveOr = 14,
//
// 摘要:
//     A "greater than" comparison, such as (a > b).
GreaterThan = 15,
//
// 摘要:
//     A "greater than or equal to" comparison, such as (a >= b).
GreaterThanOrEqual = 16,
//
// 摘要:
//     An operation that invokes a delegate or lambda expression, such as sampleDelegate.Invoke().
Invoke = 17,
//
// 摘要:
//     A lambda expression, such as a => a + a in C# or Function(a) a + a in Visual
//     Basic.
Lambda = 18,
//
// 摘要:
//     A bitwise left-shift operation, such as (a << b).
LeftShift = 19,
//
// 摘要:
//     A "less than" comparison, such as (a < b).
LessThan = 20,
//
// 摘要:
//     A "less than or equal to" comparison, such as (a <= b).
LessThanOrEqual = 21,
//
// 摘要:
//     An operation that creates a new System.Collections.IEnumerable object and initializes
//     it from a list of elements, such as new List<SampleType>(){ a, b, c } in C# or
//     Dim sampleList = { a, b, c } in Visual Basic.
ListInit = 22,
//
// 摘要:
//     An operation that reads from a field or property, such as obj.SampleProperty.
MemberAccess = 23,
//
// 摘要:
//     An operation that creates a new object and initializes one or more of its members,
//     such as new Point { X = 1, Y = 2 } in C# or New Point With {.X = 1, .Y = 2} in
//     Visual Basic.
MemberInit = 24,
View Code
 //
        // 摘要:
        //     An arithmetic remainder operation, such as (a % b) in C# or (a Mod b) in Visual
        //     Basic.
        Modulo = 25,
        //
        // 摘要:
        //     A multiplication operation, such as (a * b), without overflow checking, for numeric
        //     operands.
        Multiply = 26,
        //
        // 摘要:
        //     An multiplication operation, such as (a * b), that has overflow checking, for
        //     numeric operands.
        MultiplyChecked = 27,
        //
        // 摘要:
        //     An arithmetic negation operation, such as (-a). The object a should not be modified
        //     in place.
        Negate = 28,
        //
        // 摘要:
        //     A unary plus operation, such as (+a). The result of a predefined unary plus operation
        //     is the value of the operand, but user-defined implementations might have unusual
        //     results.
        UnaryPlus = 29,
        //
        // 摘要:
        //     An arithmetic negation operation, such as (-a), that has overflow checking. The
        //     object a should not be modified in place.
        NegateChecked = 30,
        //
        // 摘要:
        //     An operation that calls a constructor to create a new object, such as new SampleType().
        New = 31,
        //
        // 摘要:
        //     An operation that creates a new one-dimensional array and initializes it from
        //     a list of elements, such as new SampleType[]{a, b, c} in C# or New SampleType(){a,
        //     b, c} in Visual Basic.
        NewArrayInit = 32,
        //
        // 摘要:
        //     An operation that creates a new array, in which the bounds for each dimension
        //     are specified, such as new SampleType[dim1, dim2] in C# or New SampleType(dim1,
        //     dim2) in Visual Basic.
        NewArrayBounds = 33,
        //
        // 摘要:
        //     A bitwise complement or logical negation operation. In C#, it is equivalent to
        //     (~a) for integral types and to (!a) for Boolean values. In Visual Basic, it is
        //     equivalent to (Not a). The object a should not be modified in place.
        Not = 34,
View Code

(Quote節點類型是什麼?)

 //
        // 摘要:
        //     A bitwise complement or logical negation operation. In C#, it is equivalent to
        //     (~a) for integral types and to (!a) for Boolean values. In Visual Basic, it is
        //     equivalent to (Not a). The object a should not be modified in place.
        Not = 34,
        //
        // 摘要:
        //     An inequality comparison, such as (a != b) in C# or (a <> b) in Visual Basic.
        NotEqual = 35,
        //
        // 摘要:
        //     A bitwise or logical OR operation, such as (a | b) in C# or (a Or b) in Visual
        //     Basic.
        Or = 36,
        //
        // 摘要:
        //     A short-circuiting conditional OR operation, such as (a || b) in C# or (a OrElse
        //     b) in Visual Basic.
        OrElse = 37,
        //
        // 摘要:
        //     A reference to a parameter or variable that is defined in the context of the
        //     expression. For more information, see System.Linq.Expressions.ParameterExpression.
        Parameter = 38,
        //
        // 摘要:
        //     A mathematical operation that raises a number to a power, such as (a ^ b) in
        //     Visual Basic.
        Power = 39,
        //
        // 摘要:
        //     An expression that has a constant value of type System.Linq.Expressions.Expression.
        //     A System.Linq.Expressions.ExpressionType.Quote node can contain references to
        //     parameters that are defined in the context of the expression it represents.
        Quote = 40,
        //
        // 摘要:
        //     A bitwise right-shift operation, such as (a >> b).
        RightShift = 41,
View Code

 Expression:BinaryExpression、IndexExpression、MethodCallExpression、UnaryExpression、BlockExpression、GotoExpression、DynamicExpression、LambdaExpression、MemberExpression等。

ExpressionVisitor。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 我的LeetCode刷題源碼[GitHub]:https://github.com/izhoujie/Algorithmcii LeetCode 面試題 17.16. 按摩師 題目 一個有名的按摩師會收到源源不斷的預約請求,每個預約都可以選擇接或不接。在每次預約服務之間要有休息時間,因此她不能接受相 ...
  • Android終端(車載,手機)等, 需要考慮進行隨意進行各類按鍵(車載方向盤按鍵,手機硬按鍵)的操作, 測試系統對按鍵事件的響應穩定性,一般測試2小時。 準備階段 一般是用adb shell input keyevent + keyCode 來模擬按鍵事件, 比如adb shell input k ...
  • 我們知道C++中非常重要的:1.全局函數、2.普通成員函數、3.靜態成員函數。 類中的成員函數構成的重載有這幾點: 1. 構造函數的重載。 2.普通成員函數的重載。 3.靜態成員函數的重載。 例子: 1 #include <stdio.h> 2 3 class Test 4 { 5 int i; 6 ...
  • 使用python下載音樂,小白也可以寫爬蟲 **簡介:使用BeautifulSoup和request模塊進行抓取和解析,最後保存音樂(註:音樂質量是普通品質的)在這裡順便給大家推薦一個資源很全的python學習免非解答.裙 :七衣衣九七七巴而五(數字的諧音)轉換下可以找到了,這裡有資深程式員分享以前 ...
  • 結果: ...
  • 1.bytes和str函數 那我接下來就簡述下他文章的意思吧: bytes格式時二進位型的文件,全為010101之類的,而str為字元串型的 bytes函數中的參數為bytes(字元串,encoding=' 括弧裡面經常填utf 8')其中encoding一定要填,str函數則為str()與其是一樣 ...
  • 第三章 Stream流 關註公眾號( CoderBuff )回覆“ stream ”獲取《Java8 Stream編碼實戰》PDF完整版。 《Java8 Stream編碼實戰》的代碼全部在 "https://github.com/yu linfeng/BlogRepositories/tree/ma ...
  • 上次[ActionFilter引發的一個EF異常](https://www.cnblogs.com/kklldog/p/not-use-sync-in-actionfilter.html),本質上是對Core版本的ActionFilter的知識掌握不夠牢固造成的,所以花了點時間仔細閱讀了微軟的官方文... ...
一周排行
    -Advertisement-
    Play Games
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...