亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

帶你一文了解C#中的Expression

 更新時間:2021年12月13日 10:42:18   作者:RyzenAdorer  
c#中有Expression,即表達式,通過Expression可以動態(tài)構造代碼,并編譯執(zhí)行,下面這篇文章主要給大家介紹了關于C#中Expression的相關資料,需要的朋友可以參考下

前言

我們書接上文,我們在了解LINQ下面有說到在本地查詢IEnumerbale主要是用委托來作為傳參,而解析型查詢

IQueryable則用Expression來作為傳參:

public static IEnumerable<T> Where<T>(this IEnumerable<T> enumable, Func<T, bool> func)

public static IQueryable<T> Where<T>(this IQueryable<T> queryable, Expression<Func<T, bool>> func)

那么我們就來聊聊有關表達式Expression里面的東西吧

Expression與Expression Tree

首先我們來寫下一些代碼:

Expression<Func<int, int>> expression = (num) => num + 5;
Console.WriteLine($"NodeType:{expression.NodeType}");
Console.WriteLine($"Body:{expression.Body}");
Console.WriteLine($"Body Type: {expression.Body.GetType()}");
Console.WriteLine($"Body NodeType: {expression.Body.NodeType}");

輸出如下:

NodeType:Lambda

Body:(num + 5)

Body Type: System.Linq.Expressions.SimpleBinaryExpression

Body NodeType: Add

我們將expression轉為LambdaExpression看看都有啥:

if (expression.NodeType == ExpressionType.Lambda)
{
    var lambda = (LambdaExpression)expression;
    var parameter = lambda.Parameters.Single();
    Console.WriteLine($"parameter.Name:{parameter.Name}");
    Console.WriteLine($"parameter.Type:{parameter.Type}");
    Console.WriteLine($"parameter.ReturnType:{lambda.ReturnType}");
}

輸出如下:

parameter.Name:num

parameter.Type:System.Int32

parameter.ReturnType:System.Int32

由于我們知道expression.Body是BinaryExpression,那么我們就將其轉為它,然后我們繼續(xù)看下去:

if (expression.Body.NodeType == ExpressionType.Add)
{
    var binaryExpreesion = (BinaryExpression)expression.Body;

    
    Console.WriteLine($"Left Type:{binaryExpreesion.Left.GetType()}");
    Console.WriteLine($"Left NodeType:{binaryExpreesion.Left.NodeType}");

    Console.WriteLine($"Right Type:{binaryExpreesion.Right.GetType()}");
    Console.WriteLine($"Right NodeType:{binaryExpreesion.Right.NodeType}");

    if (binaryExpreesion.Left is ParameterExpression parameterExpreesion)
    {
        Console.WriteLine($"parameterExpreesion.Name:{parameterExpreesion.Name}");
        Console.WriteLine($"parameterExpreesion.Type:{parameterExpreesion.Type}");
    }

    if (binaryExpreesion.Right is ConstantExpression constantExpreesion)
    {
        Console.WriteLine($"constantExpreesion.Value:{constantExpreesion.Value}" );
    }
}

輸出如下:

Left Type:System.Linq.Expressions.PrimitiveParameterExpression`1[System.Int32]

Left NodeType:Parameter

Right Type:System.Linq.Expressions.ConstantExpression

Right NodeType:Constant

parameterExpreesion.Name:num

parameterExpreesion.Type:System.Int32

constantExpreesion.Value:5

最后我們將表達式樹轉為委托:

var @delegate = expression.Compile();
Console.WriteLine(@delegate?.Invoke(2));

輸出:

7 //2+5

實際上,通過Expression<Func<int, int>> expression = (num) => num + 5;,賦值后的expression 變成了一個表達式樹,它的結構是這樣的:

而有意思的是二元表達式樹BinaryExpression是一個二叉樹,而LambdaExpression則是一個支持參數的表達式,能夠通過其Parameters屬性知道傳入的參數的類型和數量,通過ReturnType知道返回值是什么類型

而我們再看看整個關于Expression的繼承關系鏈:

因此,我們也可以顯式的通過各自Expreesion的實現子類來創(chuàng)建跟lambda表達式一樣的結果:

var parameterExpreesion1 = Expression.Parameter(typeof(int), "num");
BinaryExpression binaryExpression1 = Expression.MakeBinary(ExpressionType.Add, parameterExpreesion1, Expression.Constant(5));
Expression<Func<int, int>> expression1 = Expression.Lambda<Func<int, int>>(binaryExpression1, parameterExpreesion1);

if (expression1.Body.NodeType == ExpressionType.Add)
{
    var binaryExpreesion1 = (BinaryExpression)expression1.Body;


    Console.WriteLine($"Left Type:{binaryExpreesion1.Left.GetType()}");
    Console.WriteLine($"Left NodeType:{binaryExpreesion1.Left.NodeType}");

    Console.WriteLine($"Right Type:{binaryExpreesion1.Right.GetType()}");
    Console.WriteLine($"Right NodeType:{binaryExpreesion1.Right.NodeType}");

    if (binaryExpreesion1.Left is ParameterExpression parameterExpreesion2)
    {
        Console.WriteLine($"parameterExpreesion.Name:{parameterExpreesion2.Name}");
        Console.WriteLine($"parameterExpreesion.Type:{parameterExpreesion2.Type}");
    }

    if (binaryExpreesion1.Right is ConstantExpression constantExpreesion1)
    {
        Console.WriteLine($"constantExpreesion.Value:{constantExpreesion1.Value}");
    }

    var @delegate1 = expression1.Compile();
    Console.WriteLine(@delegate1(2));

輸出結果:

Left Type:System.Linq.Expressions.PrimitiveParameterExpression`1[System.Int32]

Left NodeType:Parameter

Right Type:System.Linq.Expressions.ConstantExpression

Right NodeType:Constant

parameterExpreesion.Name:num

parameterExpreesion.Type:System.Int32

constantExpreesion.Value:5

result:7

我們則發(fā)現,結果是一模一樣的,但是費勁了很多,因此用lamda構建表達式樹是一個非常愉快的語法糖,讓你能夠愉快的在使用表達式和表達式樹

參考

  • 《C#7.0核心技術指南》

源碼

BlogCodeSample/ExpressionSample at main · ZhengDaoWang/BlogCodeSample

總結

到此這篇關于C#中Expression的文章就介紹到這了,更多相關C#的Expression內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C#編程實現發(fā)送郵件的方法(可添加附件)

    C#編程實現發(fā)送郵件的方法(可添加附件)

    這篇文章主要介紹了C#編程實現發(fā)送郵件的方法,具備添加附件的功能,涉及C#文件傳輸及郵件發(fā)送的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • C#使用Task實現并行編程

    C#使用Task實現并行編程

    這篇文章介紹了C#使用Task實現并行編程的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • 使用C# CefSharp Python采集某網站簡歷并且自動發(fā)送邀請短信的方法

    使用C# CefSharp Python采集某網站簡歷并且自動發(fā)送邀請短信的方法

    這篇文章主要給大家介紹了關于如何使用C# CefSharp Python采集某網站簡歷并且自動發(fā)送邀請短信的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧
    2019-03-03
  • C#中匿名方法與委托的關系介紹

    C#中匿名方法與委托的關系介紹

    這篇文章介紹了C#中匿名方法與委托的關系,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • C#實現判斷圖形文件格式的方法

    C#實現判斷圖形文件格式的方法

    這篇文章主要介紹了C#實現判斷圖形文件格式的方法,包括常見的擴展名判定及文件內容判定等,非常實用,需要的朋友可以參考下
    2014-09-09
  • C#實現控制電腦注銷,關機和重啟

    C#實現控制電腦注銷,關機和重啟

    這篇文章主要為大家介紹了C#如何實現控制電腦注銷,關機和重啟功能,本案例涉及的知識點包含:Process、Shell32.dll、User32.dll、Struct數據結構,感興趣的可以了解一下
    2022-09-09
  • 通過C#實現自動售貨機接口

    通過C#實現自動售貨機接口

    這篇文章主要介紹了通過C#實現自動售貨機接口,需要的朋友可以參考下
    2015-07-07
  • C#的String轉換成float防止精度丟失問題的解決

    C#的String轉換成float防止精度丟失問題的解決

    這篇文章主要介紹了C#的String轉換成float防止精度丟失問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Unity實現鼠標點2D轉3D進行旋轉

    Unity實現鼠標點2D轉3D進行旋轉

    這篇文章主要為大家詳細介紹了Unity實現鼠標點2D轉3D進行旋轉,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • c#實現簡單控制臺udp異步通信程序示例

    c#實現簡單控制臺udp異步通信程序示例

    這篇文章主要介紹了c#實現簡單控制臺udp異步通信程序示例,需要的朋友可以參考下
    2014-04-04

最新評論