Expression
表达式目录树
1.什么是表达式目录树Expression?
表达式目录树是一个数据结构,语法树。
首先我们去看看 Expressions类 ,定义了一个泛型委托类型 TDelegate:
// 摘要:
// 将强类型化的 Lambda 表达式表示为表达式树形式的数据结构。 此类不能被继承。
//
// 类型参数:
// TDelegate:
// 该委托的类型, System.Linq.Expressions.Expression`1 表示。
public sealed class Expression<TDelegate> : LambdaExpression
我们先来一个带返回值的委托: 其中m、n是两个Int 类型的参数
Func<int, int, int> func = (m, n) => m * n + ;
//lambda实例化委托 是个方法 是实例化委托的参数
int iResult1 =func.Invoke(1,3); //调用执行
通过表达式目录树计算 m*n+2:
Expression<Func<int, int, int>> exp = (m, n) => m * n + ;
int iResult2 = exp.Compile().Invoke(, );
//Complie()方法将编译表达式树由描述为可执行代码的 lambda 表达式,并生成一个委托,表示 lambda 表达式。所以可以调用Invoke方法。
通过中间语言IL反编译查看源码 Expression<Func<int, int, int>> exp = (m, n) => m * n + 2
表达式目录树结构拆分步骤:
手动拼装表达式目录树 m * n + m + n + 2;
{
//Expression<Func<int, int, int>> expression = (m, n) => m * n + m + n + 2;
//int iResult = expression.Compile().Invoke(1, 2); ParameterExpression m = Expression.Parameter(typeof(int), "m"); //第一个参数 M
ParameterExpression n = Expression.Parameter(typeof(int), "n"); //第二个参数 n
var constant = Expression.Constant(); //常量 2 var mutiply = Expression.Multiply(m, n); // m*n
var plus1 = Expression.Add(mutiply, m); // m*n+m
var plus2 = Expression.Add(plus1, n); //m*n+m+n
var plus3 = Expression.Add(plus2, constant); //m*n+m+n+2
Expression<Func<int, int, int>> expression = Expression.Lambda<Func<int, int, int>>(plus3, new ParameterExpression[] { m, n });
//组装表达式目录树
int iResult = expression.Compile().Invoke(, );//调用
}
Expression的更多相关文章
- AutoMapper:Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 应用场景:ViewModel==>Mode映射的时候出错 AutoMappe ...
- OpenCASCADE Expression Interpreter by Flex & Bison
OpenCASCADE Expression Interpreter by Flex & Bison eryar@163.com Abstract. OpenCASCADE provide d ...
- Expression Blend创建自定义按钮
在 Expression Blend 中,我们可以在美工板上绘制形状.路径和控件,然后修改其外观和行为,从而直观地设计应用程序.Button按钮也是Expression Blend最常用的控件之一,在 ...
- [C#] C# 知识回顾 - 表达式树 Expression Trees
C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...
- Could not evaluate expression
VS15 调试变量不能显示值,提示:Could not evaluate expression 解决办法: 选择"在调试时显示运行以单击编辑器中的按钮"重启VS即可. 可参考:Vi ...
- 使用Expression实现数据的任意字段过滤(1)
在项目常常要和数据表格打交道. 现在BS的通常做法都是前端用一个js的Grid控件, 然后通过ajax的方式从后台加载数据, 然后将数据和Grid绑定. 数据往往不是一页可以显示完的, 所以要加分页: ...
- 使用Expression实现数据的任意字段过滤(2)
上一篇<使用Expression实现数据的任意字段过滤(1)>, 我们实现了通过CriteriaCollectionHandler对象来处理集合数据过滤.通过适当的扩展, 应该可以满足一般 ...
- React Native JSX value should be expression or a quoted JSX text.
问题描述: 我在使用props时候, 我的写法是这样的 ... <View> <Person name='john' age=32 gender=true></Pers ...
- [LeetCode] Ternary Expression Parser 三元表达式解析器
Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- [Swift]LeetCode1011. 在 D 天内送达包裹的能力 | Capacity To Ship Packages Within D Days
A conveyor belt has packages that must be shipped from one port to another within D days. The i-th p ...
- github pages绑定域名
网上很多人问 github 绑定域名要不要备案,很多人的回答是: 国内主机需要备案,国外主机不用 这个说法是没错的,但是却没有直接回答出 github pages 是否需要备案! 首先声明 githu ...
- CentOS 7 源码编译安装 Nginx
这里安装的是nginx 1.14版本 1.下载源码 #下载 wget http://nginx.org/download/nginx-1.14.0.tar.gz #解压 tar -xzf nginx- ...
- C++ crash 堆栈信息获取
最近在做程序异常时堆栈信息获取相关工作,上一篇文章成功的在程序creash时写下了dump文件,而有些情况写dump文件是 不可以的,比如在jni开发时,C++只做底层处理,而整个项目是android ...
- 基于winserver部署Apollo初次体验(附.net客户端demo)
前言 配置中心伴随着这几年分布式系统演变和微服务架构的兴起,已经成为必不可少的需求之一.试下一下如果哪天公司的所有应用服务,从公司服务器迁移到云服务,成千上万的配置,修改起来是多么耗时费劲的事(我们公 ...
- 在Mac上使用远程X11应用
XWindows太老了,历史比Windows和Linux的开发时间都长,以至于很多人每天实际在用,但已经不知道它的存在. XWindows目前是Linux/类Unix系统上的标准显示配置,QT/GTK ...
- [Leetcode]695. Max Area of Island
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- [Leetcode]674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- 【Java基础】【20IO(字节流)】
20.01_IO流(IO流概述及其分类) 1.概念 IO流用来处理设备之间的数据传输 Java对数据的操作是通过流的方式 Java用于操作流的类都在IO包中 流按流向分为两种:输入流,输出流. 流按操 ...
- SpringCloud入门之常用的配置文件 application.yml和 bootstrap.yml区别
作者其他技术文章 1) Spring Boot 简介 2)SpringCloud入门之YAML格式文件规范学习 3)SpringCloud入门之Spring Boot多环境配置切换指南 4) Elas ...