C#判断语句的表达式树实现
作者:痴者工良 发布时间:2022-11-10 21:55:02
标签:C#,判断,语句,表达式树
C# 提供了以下类型的判断语句:
语句 | 描述 |
---|---|
if | 一个 if 语句 由一个布尔表达式后跟一个或多个语句组成。 |
if...else | 一个 if 语句 后可跟一个可选的 else 语句,else 语句在布尔表达式为假时执行。 |
嵌套 if 语句 | 您可以在一个 if 或 else if 语句内使用另一个 if 或 else if 语句。 |
switch 语句 | 一个 switch 语句允许测试一个变量等于多个值时的情况。 |
嵌套 switch 语 | 您可以在一个 switch 语句内使用另一个 switch 语句。 |
当然还有 ??
、?:
等判断,下面将详细实践。
if
If 语句,使用 IfThen(Expression test, Expression ifTrue);
来表达
Expression test
表示用于判断的表达式,Expression ifTrue
表示结果为 true 时执行的表达式树。
示例
int a = 10;
int b = 10;
if (a == b)
{
Console.WriteLine("a == b 为 true,语句被执行");
}
Console.ReadKey();
使用表达式树实现如下
ParameterExpression a = Expression.Variable(typeof(int), "a");
ParameterExpression b = Expression.Variable(typeof(int), "b");
MethodCallExpression call = Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
Expression.Constant("a == b 为 true,表达式树被执行"));
ConditionalExpression _if = Expression.IfThen(Expression.Equal(a, b),call);
Expression<Action<int, int>> lambda = Expression.Lambda<Action<int, int>>(_if,a,b);
lambda.Compile()(10,10);
Console.ReadKey();
生成的表达式树如下
.Lambda #Lambda1<System.Action`2[System.Int32,System.Int32]>(
System.Int32 $a,
System.Int32 $b) {
.If ($a == $b) {
.Call System.Console.WriteLine("a == b 为 true,表达式树被执行")
} .Else {
.Default(System.Void)
}
}
if...else
if...else 使用以下表达式树表示
ConditionalExpression IfThenElse(Expression test, Expression ifTrue, Expression ifFalse);
示例代码如下
int a = 10;
int b = 11;
if (a == b)
{
Console.WriteLine("a == b 为 true,此语句被执行");
}
else
{
Console.WriteLine("a == b 为 false,此语句被执行");
}
Console.ReadKey();
用表达式树实现如下
ParameterExpression a = Expression.Variable(typeof(int), "a");
ParameterExpression b = Expression.Variable(typeof(int), "b");
MethodCallExpression call1 = Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
Expression.Constant("a == b 为 true,此表达式树被执行"));
MethodCallExpression call2 = Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
Expression.Constant("a == b 为 false,此表达式树被执行"));
ConditionalExpression _if = Expression.IfThenElse(Expression.Equal(a, b), call1,call2);
Expression<Action<int, int>> lambda = Expression.Lambda<Action<int, int>>(_if, a, b);
lambda.Compile()(10, 11);
Console.ReadKey();
生成的表达式树如下
.Lambda #Lambda1<System.Action`2[System.Int32,System.Int32]>(
System.Int32 $a,
System.Int32 $b) {
.If ($a == $b) {
.Call System.Console.WriteLine("a == b 为 true,此表达式树被执行")
} .Else {
.Call System.Console.WriteLine("a == b 为 false,此表达式树被执行")
}
}
switch
示例代码如下
int a = 2;
switch (a)
{
case 1:Console.WriteLine("a == 1");break;
case 2:Console.WriteLine("a == 2");break;
default:Console.WriteLine("a != 1 && a = 2");
}
Console.ReadKey();
每个 case 使用 SwitchCase 类型表示,使用 Expression.SwitchCase 生成 SwitchCase 类型。
Expression.Switch 用来构建一个 switch 表达式树,
Expression.Switch 的重载比较多,常用的是这种形式
SwitchExpression Switch(Expression switchValue, Expression defaultBody, params SwitchCase[] cases);
switchValue 表示传入参数;
defaultBody 表示 default 执行的表达式;
cases 表示多条 case 。
上面代码对应使用表达式树编写如下
ParameterExpression a = Expression.Parameter(typeof(int), "a");
MethodCallExpression _default = Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
Expression.Constant("a != 1 && a = 2"));
SwitchCase case1 = Expression.SwitchCase(
Expression.Call(null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
Expression.Constant("a == 1")),
new ConstantExpression[] { Expression.Constant(1) }
);
SwitchCase case2 = Expression.SwitchCase(
Expression.Call(null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
Expression.Constant("a == 2")),
new ConstantExpression[] { Expression.Constant(2) }
);
SwitchExpression _switch = Expression.Switch(a, _default, new SwitchCase[] { case1, case2 });
Expression<Action<int>> lambda = Expression.Lambda<Action<int>>(_switch, a);
lambda.Compile()(1);
Console.ReadKey();
生成的表达式树如下
.Lambda #Lambda1<System.Action`1[System.Int32]>(System.Int32 $a) {
.Switch ($a) {
.Case (1):
.Call System.Console.WriteLine("a == 1")
.Case (2):
.Call System.Console.WriteLine("a == 2")
.Default:
.Call System.Console.WriteLine("a != 1 && a = 2")
}
}
很奇怪,没有 break,但是表达式树是正常的,并且运行没问题;
?? 和 ?:
?? 表示空合并运算符,例如 a ?? b
,如果 a 不为 null,即返回 a,否则返回 b;
常用定义如下
BinaryExpression Coalesce(Expression left, Expression right)
这里就不再赘述。
?: 是三元运算符,例如 a > b ? a : b 。
常用定义如下
ConditionalExpression Condition(Expression test, Expression ifTrue, Expression ifFalse)
可以参考上面的 if...else 表达式树,这里不再赘述。
来源:https://www.cnblogs.com/whuanle/p/11552311.html


猜你喜欢
- 在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Di
- 一、 * 简介 * 通常通过 * 的方式来执行。 * 的生命周期由IoC容器管理,可以通过注入等方式来获取其他Bean的实例,使用更方便。
- 本文较为详细的描述了重载运算符的方法。一般来说,重载运算符在实际的项目开发中会经常的用到,但如果某些自定义类型通过简短几行代码重载一些常用的
- 背景现行的文本编辑器大多都具备文本查询的能力,但是并不能直观的告诉用户两段文字的细微差异,所以对比工具在某种情况下,就起到了很便捷的效率。关
- 一、获取企业微信群机器人 Webhook 地址业务需要在企业微信推送告警监控或者定时提醒业务,就可以使用企业微信自带的机器人工具Webhoo
- 题目要求思路一:DFS+序列化设计一种规则将所有子树序列化,保证不同子树的序列化字符串不同,相同子树的序列化串相同。用哈希表存所有的字符串,
- 如下所示:import org.apache.commons.lang.StringUtils; public class Test {
- 约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;
- 一、什么是 websocket 接口使用 websocket 建立长连接,服务端和客户端可以互相通信,服务端只要有数据更新,就可以主动推给客
- java类的方法,我特别喜欢《java编程思想》里面的描述,这本书说java类之间的相互通信是通过消息。比如顾客类的对象调用一个eat方法,
- 形参和实参java在定义方法时可以设置参数,参数分为形参和实参,形参是指在定义函数时用于接收外部传入数据的参数,而实参是指在调用方法时主调函
- 最近碰到个需要下载zip压缩包的需求,于是我在网上找了下别人写好的zip工具类。但找了好多篇博客,总是发现有bug。因此就自己来写了个工具类
- 前言 之前的文章有介绍ActivityGroup,不少人问嵌套使用的问题,同样的需求在Fragment中也存在,幸好在最新的An
- 一、背景在上一篇文章中,我们使用Seata整合了SpringBoot,在这篇文章中我们使用Seata整合SpringCloud。同时有了上一
- 前言任何一个服务如果没有监控,那就是两眼一抹黑,无法知道当前服务的运行情况,也就无法对可能出现的异常状况进行很好的处理,所以对任意一个服务来
- 前言工作中是否有这样的场景,多个线程任务,如果所有线程完成到某个阶段,你希望知道所有线程均完成该阶段。当然你使用线程计数可以实现,只是不够优
- 我们使用控件AutoCompleteTextView 自动提示时,有时需要设置BaseAdapter,设置BaseAdapter时,需要实现
- 这篇文章主要介绍了SpringBoot加载外部依赖过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- 一、简介Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新spring应用的初始搭建以及开发过程。该框架使用
- 反射是框架设计的灵魂(使用的前提条件:必须先得到代表的字节码的Class,Class类用于表示.class文件(字节码))一、反射的概述JA