表达式树练习实践:C# 循环

C# 提供了以下几种循环类型。

循环类型 描述
while 循环 当给定条件为真时,重复语句或语句组。它会在执行循环主体之前测试条件。
for/foreach 循环 多次执行一个语句序列,简化管理循环变量的代码。
do...while 循环 除了它是在循环主体结尾测试条件外,其他与 while 语句类似。
嵌套循环 您可以在 while、for 或 do..while 循环内使用一个或多个循环。

当然,还有以下用于控制循环的语句

控制语句 描述
break 语句 终止 loopswitch 语句,程序流将继续执行紧接着 loop 或 switch 的下一条语句。
continue 语句 引起循环跳过主体的剩余部分,立即重新开始测试条件。

LabelTarget

LabelTarget 是用于创建循环标记的。

无论是 for 还是 while ,平时编写循环时,都需要有跳出循环的判断,有时需要某个参数自增自减并且作为判断依据。

C# 表达式树里面是没有专门表示 for /while 的,里面只有一个 Loop。看一下Loop 生成的表达式树

.Lambda #Lambda1<System.Func`1[System.Int32]>() {
.Block(System.Int32 $x) {
$x = 0;
.Loop {
.If ($x < 10) {
$x++
} .Else {
.Break #Label1 { $x }
}
}
.LabelTarget #Label1:
}
}

要实现循环控制,有 break,contauine 两种 Expression:

        public static GotoExpression Break(LabelTarget target, Type type);

        public static GotoExpression Break(LabelTarget target, Expression value);

        public static GotoExpression Break(LabelTarget target);

        public static GotoExpression Break(LabelTarget target, Expression value, Type type);
        public static GotoExpression Continue(LabelTarget target, Type type);

        public static GotoExpression Continue(LabelTarget target);

所以,要实现循环控制,必须要使用 LabelTarget,不然就无限循环了。

要理解 LabelTarget ,最好的方法是动手做。

for / while 循环

Expression.Loop 用于创建循环,包括 for 和 while,定义如下

        public static LoopExpression Loop(Expression body, LabelTarget @break, LabelTarget @continue);

      System.Linq.Expressions.LoopExpression.
public static LoopExpression Loop(Expression body); public static LoopExpression Loop(Expression body, LabelTarget @break);

表达式树里面的循环,只有 Loop,无 for / while 的区别。

那么,我们来一步步理解 Loop 循环和 LabelTarget;

无限循环

                while (true)
{
Console.WriteLine("无限循环");
}

那么,对应的 Loop 重载是这种

public static LoopExpression Loop(Expression body)

使用表达式树编写

            BlockExpression _block = Expression.Block(
new ParameterExpression[] { },
Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),Expression.Constant("无限循环") )
); LoopExpression _loop = Expression.Loop(_block); Expression<Action> lambda = Expression.Lambda<Action>(_loop);
lambda.Compile()();

最简单的循环

如果我想用表达式树做到如下最简单的循环,怎么写?

            while (true)
{
Console.WriteLine("我被执行一次就结束循环了");
break;
}

表达式树编写

            LabelTarget _break = Expression.Label();

            BlockExpression _block = Expression.Block(
new ParameterExpression[] { },
Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("我被执行一次就结束循环了")), Expression.Break(_break));
LoopExpression _loop = Expression.Loop(_block, _break); Expression<Action> lambda = Expression.Lambda<Action>(_loop);
lambda.Compile()(); Console.ReadKey();

生成的表达式树

.Lambda #Lambda1<System.Action>() {
.Loop {
.Block() {
.Call System.Console.WriteLine("我被执行一次就结束循环了");
.Break #Label1 { }
}
}
.LabelTarget #Label1:
}

首先要明确,Expression.Label() 里面可以为空,它是一种标记,不参与传递参数,不参与运算。有参无参,前后保持一致即可。

但是上面的循环只有一次,你可以将上面的标签改成这样试试 LabelTarget _break = Expression.Label(typeof(int));,原因后面找。

还有, Expression.Label() 变量需要一致,否则无法跳出。

试试一下代码

            BlockExpression _block = Expression.Block(
new ParameterExpression[] { },
Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("我被执行一次就结束循环了")), Expression.Break(Expression.Label()));
LoopExpression _loop = Expression.Loop(_block, Expression.Label()); Expression<Action> lambda = Expression.Lambda<Action>(_loop);
lambda.Compile()(); Console.ReadKey();

里面用到了 Expression.Block(),Block() 是块,即{}。

如果 Block() 是在最外层,那么相当于是函数;如果是内嵌,相当于{};

但不是真的这样。。。表达式树里面不是完全按照 C# 的语法来还原操作的。

对于 Block() 的使用,多加实践即可。

多次循环

写一个循环十次的循环语句

            for (int i = 0; i < 10; i++)
{
if (i < 10)
{
Console.WriteLine(i);
}
else
break;
}

或者使用 while 表示

            int i = 0;
while (true)
{
if (i < 10)
{
Console.WriteLine(i);
}
else
break;
i++;
}

使用表达式树编写

            LabelTarget _break = Expression.Label(typeof(int));
ParameterExpression a = Expression.Variable(typeof(int), "a"); BlockExpression _block = Expression.Block(new ParameterExpression[] { },
Expression.IfThenElse
(
Expression.LessThan(a, Expression.Constant(10)),
Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), a),
Expression.Break(_break, a)
),
Expression.PostIncrementAssign(a) // a++
); LoopExpression _loop = Expression.Loop(_block, _break); Expression<Action<int>> lambda = Expression.Lambda<Action<int>>(_loop, a);
lambda.Compile()(0);
Console.ReadKey();

生成的表达式树如下

.Lambda #Lambda1<System.Action`1[System.Int32]>(System.Int32 $a) {
.Loop {
.Block() {
.If ($a < 10) {
.Call System.Console.WriteLine($a)
} .Else {
.Break #Label1 { $a }
};
$a++
}
}
.LabelTarget #Label1:
}

试试将 Expression.Break(_break, a) 改成 Expression.Break(_break)。看看报什么错。。。

解决方法是,上面的标记也改成 LabelTarget _break = Expression.Label();

就跟你写代码写注释一样,里面的东西是为了让别人看代码是容易理解。

有些同学纠结于 Expression.Label(有参或无参);Expression.Break(_break, a)Expression.Break(_break),只要看看最终生成的表达式树就清楚了。

break 和 continue 一起

C# 循环代码如下

            int i = 0;
while (true)
{
if (i < 10)
{
if (i % 2 == 0)
{
Console.Write("i是偶数:");
Console.WriteLine(i);
i++;
continue;
}
Console.WriteLine("其他任务 --");
Console.WriteLine("其他任务 --");
}
else break;
i++;
}

使用 C# 表达式树编写(笔者将步骤详细拆分了,所以代码比较长)

            ParameterExpression a = Expression.Variable(typeof(int), "a");

            LabelTarget _break = Expression.Label();
LabelTarget _continue = Expression.Label(); // if (i % 2 == 0)
// {
// Console.Write("i是偶数:");
// Console.WriteLine(i);
// i++;
// continue;
// }
ConditionalExpression _if = Expression.IfThen(
Expression.Equal(Expression.Modulo(a, Expression.Constant(2)), Expression.Constant(0)),
Expression.Block(
new ParameterExpression[] { },
Expression.Call(null, typeof(Console).GetMethod("Write", new Type[] { typeof(string) }), Expression.Constant("i是偶数:")),
Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), a),
Expression.PostIncrementAssign(a),
Expression.Continue(_continue)
)
); // if (i % 2 == 0)
// {
// Console.Write("i是偶数:");
// Console.WriteLine(i);
// i++;
// continue;
// }
// Console.WriteLine("其他任务 --");
// Console.WriteLine("其他任务 --");
BlockExpression block1 = Expression.Block(
new ParameterExpression[] { },
_if,
Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("其他任务 --")),
Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("其他任务 --"))
); // if (i < 10)
// {
// if (i % 2 == 0)
// {
// Console.Write("i是偶数:");
// Console.WriteLine(i);
// i++;
// continue;
// }
// Console.WriteLine("其他任务 --");
// Console.WriteLine("其他任务 --");
// }
// else break;
ConditionalExpression if_else = Expression.IfThenElse(
Expression.LessThan(a, Expression.Constant(10)),
block1,
Expression.Break(_break)
); // if (i < 10)
// {
// if (i % 2 == 0)
// {
// Console.Write("i是偶数:");
// Console.WriteLine(i);
// i++;
// continue;
// }
// Console.WriteLine("其他任务 --");
// Console.WriteLine("其他任务 --");
// }
// else break;
// i++ ; BlockExpression block2 = Expression.Block(
new ParameterExpression[] { },
if_else,
Expression.PostIncrementAssign(a)
);
// while(true)
LoopExpression loop = Expression.Loop(block2, _break, _continue); Expression<Action<int>> lambda = Expression.Lambda<Action<int>>(loop, a);
lambda.Compile()(0);
Console.ReadKey();

生成的表达式树如下

.Lambda #Lambda1<System.Action`1[System.Int32]>(System.Int32 $a) {
.Loop .LabelTarget #Label1: {
.Block() {
.If ($a < 10) {
.Block() {
.If (
$a % 2 == 0
) {
.Block() {
.Call System.Console.Write("i是偶数:");
.Call System.Console.WriteLine($a);
$a++;
.Continue #Label1 { }
}
} .Else {
.Default(System.Void)
};
.Call System.Console.WriteLine("其他任务 --");
.Call System.Console.WriteLine("其他任务 --")
}
} .Else {
.Break #Label2 { }
};
$a++
}
}
.LabelTarget #Label2:
}

为了便于理解,上面的代码拆分了很多步。

来个简化版本

            ParameterExpression a = Expression.Variable(typeof(int), "a");

            LabelTarget _break = Expression.Label();
LabelTarget _continue = Expression.Label(); LoopExpression loop = Expression.Loop(
Expression.Block(
new ParameterExpression[] { },
Expression.IfThenElse(
Expression.LessThan(a, Expression.Constant(10)),
Expression.Block(
new ParameterExpression[] { },
Expression.IfThen(
Expression.Equal(Expression.Modulo(a, Expression.Constant(2)), Expression.Constant(0)),
Expression.Block(
new ParameterExpression[] { },
Expression.Call(null, typeof(Console).GetMethod("Write", new Type[] { typeof(string) }), Expression.Constant("i是偶数:")),
Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }), a),
Expression.PostIncrementAssign(a),
Expression.Continue(_continue)
)
),
Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("其他任务 --")),
Expression.Call(null, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("其他任务 --"))
),
Expression.Break(_break)
),
Expression.PostIncrementAssign(a)
),
_break,
_continue
); Expression<Action<int>> lambda = Expression.Lambda<Action<int>>(loop, a);
lambda.Compile()(0);
Console.ReadKey();

需要注意的是,Expression.Break Expression.Continue 有所区别。

当标签实例化都是 Expression.Label() 时,

Expression.Break(label);
Expression.Continu(label);

区别在于 continu 只能用 Expression.Label()。

Break 可以这样

LabelTarget label = Expression.Label ( typeof ( int ) );
ParameterExpression a = Expression.Variable(typeof(int), "a"); Expression.Break ( label , a )

表达式树练习实践:C# 循环与循环控制的更多相关文章

  1. 表达式树练习实践:C# 五类运算符的表达式树表达

    目录 表达式树练习实践:C# 运算符 一,算术运算符 + 与 Add() - 与 Subtract() 乘除.取模 自增自减 二,关系运算符 ==.!=.>.<.>=.<= 三 ...

  2. 表达式树练习实践:C#值类型、引用类型、泛型、集合、调用函数

    目录 表达式树练习实践:C#值类型.引用类型.泛型.集合.调用函数 一,定义变量 二,访问变量/类型的属性字段和方法 1. 访问属性 2. 调用函数 三,实例化引用类型 四,实例化泛型类型于调用 五, ...

  3. 表达式树练习实践:C#判断语句

    目录 表达式树练习实践:C#判断语句 if if...else switch ?? 和 ?: 表达式树练习实践:C#判断语句 判断语句 C# 提供了以下类型的判断语句: 语句 描述 if 一个 if ...

  4. 用五分钟重温委托,匿名方法,Lambda,泛型委托,表达式树

    这些对老一代的程序员都是老生常谈的东西,没什么新意,对新生代的程序员却充满着魅力.曾经新生代,好多都经过漫长的学习,理解,实践才能掌握委托,表达式树这些应用.今天我尝试用简单的方法叙述一下,让大家在五 ...

  5. 干货!表达式树解析"框架"(1)

    最新设计请移步 轻量级表达式树解析框架Faller http://www.cnblogs.com/blqw/p/Faller.html 关于我和表达式树 其实我也没有深入了解表达式树一些内在实现的原理 ...

  6. C# Lambda表达式详解,及Lambda表达式树的创建

    最近由于项目需要,刚刚学完了Action委托和Func<T>委托,发现学完了委托就必须学习lambda表达式,委托和Lambda表达式联合起来,才能充分的体现委托的便利.才能使代码更加简介 ...

  7. 转帖:用五分钟重温委托,匿名方法,Lambda,泛型委托,表达式树

    用五分钟重温委托,匿名方法,Lambda,泛型委托,表达式树 这些对老一代的程序员都是老生常谈的东西,没什么新意,对新生代的程序员却充满着魅力.曾经新生代,好多都经过漫长的学习,理解,实践才能掌握委托 ...

  8. LinQ实战学习笔记(三) 序列,查询操作符,查询表达式,表达式树

    序列 延迟查询执行 查询操作符 查询表达式 表达式树 (一) 序列 先上一段代码, 这段代码使用扩展方法实现下面的要求: 取进程列表,进行过滤(取大于10M的进程) 列表进行排序(按内存占用) 只保留 ...

  9. 16.C#初见Lambda表达式及表达式树(九章9.1-9.3)

    在说明Lambda相关知识前,我们需要了解Lambda表达式常用于LINQ,那么我们来聊下LINQ. LINQ的基本功能就是创建操作管道,以及这些操作需要的任何状态.这些操作表示了各种关于数据的逻辑: ...

随机推荐

  1. 分布式存储——ceph 的 python 基础接口

    python 使用 boto 库完成分布式存储读.写.判断接口 import boto import boto.s3.connection from boto.s3.key import Key im ...

  2. git码云的使用基础(为了以后更好的协同操作)

    git手册 安装教程 windows 不需要什么操作点点点就好 设置一个文件夹当成本地仓库 第一次上传 git init# 创建一个本地的仓库 git add. 当前文件夹下所有内容# 添加到暂存区 ...

  3. js 事件发布订阅销毁

    在vue中 通过$on订阅事件,通过$emit触发事件以此可用来事件跨组件传值等功能,但是有个弊端就是通过这种方式订阅的事件可能会触发多次. 特别是通过$on订阅的事件中如果有http请求,将会造成触 ...

  4. 使用Cmake编译CEF时遇到Error in configuration process,project file may be invalid的解决办法

    今天在用Cmake编译cef框架时,弹出了错误,如图: 可以排查一下几种原因: 1.在64位计算机编译32位程序 可以更换编译环境,或者下载64位版本来解决这个问题. 2.选择的Visual Stud ...

  5. Python day01 课堂笔记

    今天是第一天学习Python课程,主要从计算机基础,Python的历史,环境 ,变量,常量,注释,用户交互,基础数据类型 ,简单的if条件语句和while循环语句这几个来学习,重点的掌握内容是pyth ...

  6. Spring源码剖析9:Spring事务源码剖析

    转自:http://www.linkedkeeper.com/detail/blog.action?bid=1045 声明式事务使用 Spring事务是我们日常工作中经常使用的一项技术,Spring提 ...

  7. 消息中间件——RabbitMQ(七)高级特性全在这里!(上)

    前言 前面我们介绍了RabbitMQ的安装.各大消息中间件的对比.AMQP核心概念.管控台的使用.快速入门RabbitMQ.本章将介绍RabbitMQ的高级特性.分两篇(上/下)进行介绍. 消息如何保 ...

  8. ssh-key生成密钥及SSH无密码登录的配置

    文章作者:foochane  原文链接:https://foochane.cn/article/2019061601.html 1 ssh-keygen命令 ssh-keygen命令说明: -t :指 ...

  9. Gym - 101252H

    题意略. 思路:二分.注意当利率高且m比较小的时候,每个月的偿还可能会大于本金,所以我们二分的右边界应该要设为2 * 本金. 详见代码: #include<bits/stdc++.h> # ...

  10. python 生成式,生成器,迭代器,可迭代

    1.生成器就是迭代器2.生成式用[](中括号)包起来,生成器用()(圆括号)包起来3.生成式在使用前已经在内存中存在,生成器只有在使用时才生成4.迭代器是可以用.__next__()不断取出下一值,直 ...