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
0
投稿
猜你喜欢
- layout布局<RelativeLayout xmlns:android="http://schemas.android.
- 序列化与反序列化序列化:把对象转换成字节的过程,称为对象序列化反序列化:把字节恢复成对象的过程,称为反序列化对象的持久化概念:把字节保存的硬
- 在互联网的服务端开发的时候,我们很经常要在一个项目中去调用不同的数据库。在这种情况下,必然要涉及到多数据源问题。那么,我们该如何解决多数据源
- 一.你了解类吗?在Java中,类文件是以.java为后缀的代码文件,在每个类文件中最多只允许出现一个public类,当有public类的时候
- 目录通过Resource接口手动加载通过@Value自动转换通过ResourceLoader加载使用ResourceUtils加载资源读取资
- volatile先看个例子class Test {// 定义一个全局变量 private boolean isRu
- 中午没事,把去年刚毕业那会画的几张图翻出来了,大概介绍Winform应用程序运行的过程,以及TCP协议在Winform中的应用。如果有Win
- 本文实例中的自定义类PictureBox继承于UserControl,最终实现简单的分屏功能。分享给大家供大家参考之用。具体实现代码如下:p
- 最近研究了一下android摄像头开发相关的技术,也看了Google提供的Camera2Basic调用示例,以及网上一部分代码,但都是在Te
- 什么是Handler?Handler可以发送和处理消息对象或Runnable对象,这些消息对象和Runnable对象与一个线程相关联。每个H
- 使用lombok插件的好处我们在java开发过程中,经常会有一些常规性的,重复性的工作。比如:根据成员变量生成get和set方法根据成员变量
- C语言数据结构实现银行模拟实现代码:#include <stdio.h> #include <stdlib.h> #
- //创建excelobject missing = System.Reflection.Missing.Value;Excel.Applic
- 实例如下所示:public class JsonExtracter { public static void main(String[] a
- 前言近期一直在忙项目,我也是打工仔。不多说,我们开始玩一玩seata。什么都不说,我们按照惯例,先上一个图(图里不规范的使用请忽略):简单一
- JavaConfig,是在 Spring 3.0 开始从一个独立的项目并入到 Spring 中的。JavaConfig 可以看成一个用于完成
- 多文件上传就是改良一个方法把MultipartFile类换成CommonsMultipartFile类,因为上传多个文件用数组方式的话Mul
- 零、动态库 System.Math.dll引入动态库 using System.Math;Math 为通用数学函数、对数函数、三角函数等提供
- 一、原因:forceclose,意为强行关闭,当前应用程序发生了冲突。NullPointExection(空指针),IndexOutOfBo
- 首先,必须要强调的一点,MD5不是加密算法,而是消息摘要算法,具有不可逆性。字符串通过MD5处理后会生成128位的二进制串。我们通常会将其转