activity 表达式怎么解析expression表达式

The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.首先回顾一下上篇文章的内容,上篇文章介绍了表达式树的解析和编译。如果忘记了,可以通过下面系列文章提供的入口进行复习。这篇文章将介绍常见的表达式类型。
常见的表达式类型都有个共同的基类Expression。创建这些类型的对象,是通过API的方式创建的(也就是Expression的静态方法),首先引入命名空间:
1 using System.Linq.E
变量表达式
在表达式树中使用ParameterExpression或者ParameterExpression表达式表示变量类型,下面看一个例子,我们定义一个int类型的变量i:
// ParameterExpression表示命名的参数表达式。
&ParameterExpression i = Expression.Parameter(typeof(int),"i");
1 ParameterExpression j = Expression.Variable(typeof(int), "j");
通过f12转到定义,发现这两个方法的注释几乎是一样的。静态方法Parameter第一个参数:定义的参数类型,第二个参数:为参数名称。
常量表达式
在表达式树中使用ConstantExpression表达式表示具有常量值的表达式。,看一个例子,我们定义一个int类型的常量5.并将该值赋值给上面定义的变量i
// ParameterExpression表示命名的参数表达式。
ParameterExpression i = Expression.Parameter(typeof(int), "i");
//ParameterExpression j = Expression.Variable(typeof(int), "j");
ConstantExpression constExpr = Expression.Constant(5, typeof(int));
// 创建一个表示赋值运算的 System.Linq.Expressions.BinaryExpression
//表示包含二元运算符的表达式。
BinaryExpression binaryExpression = Expression.Assign(i, constExpr);
Constrant方法第一个参数:常量,第二个参数为什么类型的常量。
这里提到了BinaryExpression表达式,该表达式标识包含二元运算符的表达式,类似与=,&这样的二元表达式都可以使用BinaryExpression表达式来表示。
调试模式下,在自动窗口查看当前表达式的DebugView属性,这个属性在调试表达式树的时候是非常有用的:
二元表达式:
通过观察上面的图,可知变量调试模式下DebugView属性将显示前面带有&$&符号的 ParameterExpression 变量名称。那么如果参数没有名称,则会为其分配一个自动生成的名称,例如 $var1 或 $var2(这里不再举例)。
条件表达式
在很多时候,我们都需要使用条件表达式来过滤一些数据,然后返回满足条件的数据,在表达式中有这样一些表达式满足你的需求。
常见运算符
if....then:如果满足条件那么..
if...then...else:如果满足条件执行某某代码,否则执行另外的逻辑
IfThenElse方法
1 public static ConditionalExpression IfThenElse(
Expression test,
Expression ifTrue,
Expression ifFalse
bool test = true;
ConditionalExpression codition = Expression.IfThenElse(
Expression.Constant(test),
//如果条件为true,调用WriteLine方法输出&条件为true&
Expression.Call(
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
Expression.Constant("条件为true")
//如果条件false
Expression.Call(
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
Expression.Constant("条件为false")
//编译表达式树,输出结果
Expression.Lambda&Action&(codition).Compile()();
&例子描述:条件test包装为常量表达式,因为test为true,所以执行iftrue的表达式,并调用WriteLine方法打印出信息。
赋值表达式
还以上面为变量i赋值的例子为例
ParameterExpression i = Expression.Parameter(typeof(int), "i");
//ParameterExpression j = Expression.Variable(typeof(int), "j");
ConstantExpression constExpr = Expression.Constant(5, typeof(int));
// 创建一个表示赋值运算的 System.Linq.Expressions.BinaryExpression
//表示包含二元运算符的表达式。
BinaryExpression binaryExpression = Expression.Assign(i, constExpr);
1 BinaryExpression b2 = Expression.AddAssign(i, constExpr);
1 BinaryExpression b3 = Expression.SubtractAssign(i, constExpr);
BinaryExpression b4 = Expression.MultiplyAssign(i, constExpr);
1 BinaryExpression b5= Expression.DivideAssign(i, constExpr);
举一个例子
ParameterExpression i = Expression.Parameter(typeof(int), "i");
BlockExpression block = Expression.Block(
new[] { i },
//赋初值 i=5
Expression.Assign(i, Expression.Constant(5, typeof(int))),
Expression.AddAssign(i, Expression.Constant(5, typeof(int))),
Expression.SubtractAssign(i, Expression.Constant(5, typeof(int))),
Expression.MultiplyAssign(i, Expression.Constant(5, typeof(int))),
Expression.DivideAssign(i, Expression.Constant(5, typeof(int)))
Console.WriteLine(Expression.Lambda&Func&int&&(block).Compile()());
二元运算符表达式
在上面也提到了部分二元运算符表达式,类似加减乘除这样的运算符,对于二元运算符,就不再举例。这些返回的表达式树,都可以使用BinaryExpression来接收,或者使用基类Expression接收,或者更省事,使用var关键字。
一元运算符表达式
类似++,--运算符
i++等价于i=i+1,运算顺序就是i先加1,然后再赋值给i。在表达式书中使用Expression的PostIncrementAssign方法来进行自增或者自减操作。返回结果为UnaryExpression类型,同样可以使用基类Expression接收,或者var。
循环表达式
在表达式树中使用Expression的Loop方法实现循环。
在前面的文章中,也说了不能使用Lambda方式创建带块级的表达式树,不然会有如下的错误
通过API的方式可以创建块级表达式树,其中Expression的Block方法功不可没。例如上面的加减乘除的例子中,可以包括多个Expression。
那么,下面就举一个包含自增的一元表达式,循环的表达式块,并输出结果。
输出1-100之间的所有偶数。
class Program
static void Main(string[] args)
ParameterExpression i = Expression.Parameter(typeof(int), "i");
//跳出循环
LabelTarget label = Expression.Label();
BlockExpression block = Expression.Block(
new[] { i },
//为i赋初值
Expression.Assign(i, Expression.Constant(1, typeof(int))),
Expression.Loop(
Expression.IfThenElse(
//如果i&=100
Expression.LessThanOrEqual(i, Expression.Constant(100, typeof(int))),
//如果为true.进入循环体
Expression.Block(
Expression.IfThen(
//条件i%2==0;
Expression.Equal(Expression.Modulo(i, Expression.Constant(2, typeof(int))),
Expression.Constant(0, typeof(int))),
Expression.Call(typeof(Console).GetMethod("WriteLine",
new Type[] { typeof(int) }), new[] { i })),
Expression.PostIncrementAssign(i)
//如果i&100
Expression.Break(label)),
Expression.Lambda&Action&(block).Compile()();
Console.Read();
本篇文章介绍了几种常见的表达式类型,当然,还有很多并没有列出,比如switch case,try catch等。如果在项目中需要创建复杂的表达式树,Expression的静态方法Block是必不可少的。希望通过本篇的学习,对你了解Expression有所帮助。
/zh-cn/library/dd323961(v=vs.110).aspx
/zh-cn/library/bb397951.aspx
阅读(...) 评论()The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.

我要回帖

更多关于 activity 表达式 的文章

 

随机推荐