-
了解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
,那么我们就将其转为它,然后我们继续看下去:
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的实现子类来创建跟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
我们则发现,结果是一模一样的,但是费劲了很多,因此用lamda构建表达式树是一个非常愉快的语法糖,让你能够愉快的在使用表达式和表达式树
参考#
- 《C#7.0核心技术指南》
源码#
https://github.com/ZhengDaoWang/BlogCodeSample/tree/main/ExpressionSample
出 处:https://www.cnblogs.com/ryzen/p/15674630.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数