惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
C
Cisco Blogs
博客园 - 叶小钗
P
Privacy International News Feed
Jina AI
Jina AI
Apple Machine Learning Research
Apple Machine Learning Research
T
Threatpost
IT之家
IT之家
博客园 - 聂微东
Know Your Adversary
Know Your Adversary
Help Net Security
Help Net Security
罗磊的独立博客
I
Intezer
S
Schneier on Security
博客园_首页
C
CERT Recently Published Vulnerability Notes
雷峰网
雷峰网
Cisco Talos Blog
Cisco Talos Blog
宝玉的分享
宝玉的分享
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Webroot Blog
Webroot Blog
TaoSecurity Blog
TaoSecurity Blog
MyScale Blog
MyScale Blog
P
Privacy & Cybersecurity Law Blog
T
The Exploit Database - CXSecurity.com
PCI Perspectives
PCI Perspectives
Security Latest
Security Latest
H
Heimdal Security Blog
S
Secure Thoughts
Hacker News: Ask HN
Hacker News: Ask HN
Y
Y Combinator Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Microsoft Security Blog
Microsoft Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
SecWiki News
SecWiki News
The GitHub Blog
The GitHub Blog
A
Arctic Wolf
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
T
Threat Research - Cisco Blogs
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - sdxd.bgl

将SQL Server中的数据显示到SharePoint中 拓展训练总结 在PowerShell中,如何获取SharePoint的默认数据库服务器 在SharePoint页面中如何显示来自其他网站的List 恢复导出XSLT List View WebPart 实战部署FAST Search Server 2010 for SharePoint Sharepoint开发中代码运行权限级别 在InfoPath中如何获取当前用户的信息(Profile) 晒晒我的家乡 关于模式的思考 小心委托 利用System.Linq.Expressions实现四则运算计算器(三) 利用System.Linq.Expressions实现四则运算计算器(二) 利用System.Linq.Expressions实现四则运算计算器(一) 请拆招:将两个已排序集合分解成两个独立部分的集合和一个共有部分的集合? 如何实现Windows服务 用javascript实现下拉列表的自动筛选功能 - sdxd.bgl - 博客园 我的生活 电脑是个神奇的东西!
初识System.Linq.Expressions
sdxd.bgl · 2007-08-27 · via 博客园 - sdxd.bgl
 

初识System.Linq.Expressions

本文介绍.Net Framework 3.5中,命名空间System.Linq.Expressions下有关类的使用。

我们先来了解下Lambda表达式,在VS2008中,Lambda被编译成两种形式:一种是IL语言,另一种是Expression,即本文介绍的表达式。实现这两种方式,是出于不同用途的考虑。像Linq To SQL中,Lambda表达式在执行的过程中,被翻译成SQL语句并执行,而在普通的Lambda表达式中,则是和其他表达式相同,翻译成二进制代码执行。

看下面的Lambda表达式:

Func<int> func1 = ()=> 1+1;

int result1 = func1();

这个表达式非常简单,就是计算1+1Lambda表达式返回的是Delegate,如上,这个Delegate就是Func<int>。在使用这个Lambda表达式时候,我们就调用Delegate

编译器的工作,是把上面的代码翻译成下面的代码:

ConstantExpression exp1 = Expression.Constant(1);

ConstantExpression exp2 = Expression.Constant(1);

BinaryExpression exp3 = Expression.Add(exp1, exp2);

Expression<Func<int>> exp4 = Expression.Lambda<Func<int>>(exp3);

Func<int> func = exp4.Compile();

int result = func();

先把1+1翻译成两个常量表达式ConstantExpression exp1, exp2,然后将这两个表达式加起来,变成一个二元表达式BinaryExpression,再把这个二元表达式转换成Lambda表达式。可以说,前面4行和第一段代码的第1行等号右边的Lambda表达式等同。

为了得到可以执行的代码,需要把表达式编译,第5行编译并返回编译后的代理func

通过反编译System.Core.dll,可以看到,这里编译其实是利用动态方法技术,生成一个代理。

代码在编译以后是固定不变的,而表达式我们可以随意组合,这样,我们通过各种方式组合表达式,来灵活实现各种业务。比如在软件中实现公式功能,自定义查询等。

接下来我将会利用表达式,编写一个四则计算器:

欢迎关注!