yield return Il代码讲解
反编译后,迭代器用的是状态机,栈本身就是状态机,由于协程本身也有栈, 我怀疑C#中的 迭代器和基于任务的异步编程是协程(未经过验证)
.class nested private auto ansi sealed beforefieldinit '<GetEnumerator>d__12'
extends [System.Runtime]System.Object
implements class [System.Runtime]System.Collections.Generic.IEnumerator`1<object>,
[System.Runtime]System.Collections.IEnumerator,
[System.Runtime]System.IDisposable
//这个位置相当于以上等于 private class sealed <GetEnumerator>d__12:object,IEnumerator,IEnumerator,IDisposable
// nested :嵌套
//implements :实现接口
//extends:是继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承,
beforefieldinit:beforefieldinit指示CLI在调用静态方法之前不必初始化类型。
{
.custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
自定义实例:.custom 声明,后跟类型的方法声明 构造函数
将 CompilerGeneratedAttribute 特性应用到任何应用程序元素,以指示该元素由编译器生成。
// Fields 字段 .field private int32 '<>1__state' .field private object '<>2__current' .field public class School.ClassOfStudent '<>4__this' .field private int32 '<index>5__1' // Methods
//
.method public hidebysig specialname rtspecialname
instance void .ctor (
int32 '<>1__state'
) cil managed
//cil managed:托管代码
{
.custom instance void [System.Runtime]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2369
// Code size 15 (0xf)
.maxstack 8 // {
IL_0000: ldarg.0
// (no C# code)
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
// this.<>1__state = <>1__state;
IL_0006: nop
IL_0007: ldarg.0
IL_0008: ldarg.1
IL_0009: stfld int32 School.ClassOfStudent/'<GetEnumerator>d__12'::'<>1__state'
// }
IL_000e: ret
} // end of method '<GetEnumerator>d__12'::.ctor .method private final hidebysig newslot virtual
instance void System.IDisposable.Dispose () cil managed
{
.custom instance void [System.Runtime]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = (
01 00 00 00
)
.override method instance void [System.Runtime]System.IDisposable::Dispose()
// Method begins at RVA 0x2379
// Code size 1 (0x1)
.maxstack 8 // }
IL_0000: ret
} // end of method '<GetEnumerator>d__12'::System.IDisposable.Dispose .method private final hidebysig newslot virtual
instance bool MoveNext () cil managed
{
.override method instance bool [System.Runtime]System.Collections.IEnumerator::MoveNext()
// Method begins at RVA 0x237c
// Code size 133 (0x85)
.maxstack 3
.locals init (
[0] int32,
[1] int32,
[2] bool
) // switch (<>1__state)
IL_0000: ldarg.0
IL_0001: ldfld int32 School.ClassOfStudent/'<GetEnumerator>d__12'::'<>1__state'
IL_0006: stloc.0
// (no C# code)
IL_0007: ldloc.0
IL_0008: brfalse.s IL_0012 IL_000a: br.s IL_000c IL_000c: ldloc.0
IL_000d: ldc.i4.1
IL_000e: beq.s IL_0014 IL_0010: br.s IL_0016 IL_0012: br.s IL_0018 // return false;
IL_0014: br.s IL_004f IL_0016: ldc.i4.0
IL_0017: ret // <>1__state = -1;
IL_0018: ldarg.0
IL_0019: ldc.i4.m1
IL_001a: stfld int32 School.ClassOfStudent/'<GetEnumerator>d__12'::'<>1__state'
// <index>5__1 = 0;
IL_001f: nop
IL_0020: ldarg.0
IL_0021: ldc.i4.0
IL_0022: stfld int32 School.ClassOfStudent/'<GetEnumerator>d__12'::'<index>5__1'
// break;
IL_0027: br.s IL_0067 // <>2__current = <>4__this.students[<index>5__1];
IL_0029: nop
IL_002a: ldarg.0
IL_002b: ldarg.0
IL_002c: ldfld class School.ClassOfStudent School.ClassOfStudent/'<GetEnumerator>d__12'::'<>4__this'
IL_0031: ldfld class [System.Collections]System.Collections.Generic.List`1<class School.Student> School.ClassOfStudent::students
IL_0036: ldarg.0
IL_0037: ldfld int32 School.ClassOfStudent/'<GetEnumerator>d__12'::'<index>5__1'
IL_003c: callvirt instance !0 class [System.Collections]System.Collections.Generic.List`1<class School.Student>::get_Item(int32)
IL_0041: stfld object School.ClassOfStudent/'<GetEnumerator>d__12'::'<>2__current'
// <>1__state = 1;
IL_0046: ldarg.0
IL_0047: ldc.i4.1
IL_0048: stfld int32 School.ClassOfStudent/'<GetEnumerator>d__12'::'<>1__state'
// return true;
IL_004d: ldc.i4.1
IL_004e: ret // <>1__state = -1;
IL_004f: ldarg.0
IL_0050: ldc.i4.m1
IL_0051: stfld int32 School.ClassOfStudent/'<GetEnumerator>d__12'::'<>1__state'
// <index>5__1++;
IL_0056: nop
IL_0057: ldarg.0
IL_0058: ldfld int32 School.ClassOfStudent/'<GetEnumerator>d__12'::'<index>5__1'
IL_005d: stloc.1
// if (<index>5__1 < <>4__this.students.Count)
IL_005e: ldarg.0
IL_005f: ldloc.1
IL_0060: ldc.i4.1
IL_0061: add
IL_0062: stfld int32 School.ClassOfStudent/'<GetEnumerator>d__12'::'<index>5__1' IL_0067: ldarg.0
IL_0068: ldfld int32 School.ClassOfStudent/'<GetEnumerator>d__12'::'<index>5__1'
IL_006d: ldarg.0
IL_006e: ldfld class School.ClassOfStudent School.ClassOfStudent/'<GetEnumerator>d__12'::'<>4__this'
IL_0073: ldfld class [System.Collections]System.Collections.Generic.List`1<class School.Student> School.ClassOfStudent::students
IL_0078: callvirt instance int32 class [System.Collections]System.Collections.Generic.List`1<class School.Student>::get_Count()
IL_007d: clt
IL_007f: stloc.2
IL_0080: ldloc.2
IL_0081: brtrue.s IL_0029 // return false;
IL_0083: ldc.i4.0
IL_0084: ret
} // end of method '<GetEnumerator>d__12'::MoveNext .method private final hidebysig specialname newslot virtual
instance object 'System.Collections.Generic.IEnumerator<System.Object>.get_Current' () cil managed
{
.custom instance void [System.Runtime]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = (
01 00 00 00
)
.override method instance !0 class [System.Runtime]System.Collections.Generic.IEnumerator`1<object>::get_Current()
// Method begins at RVA 0x240d
// Code size 7 (0x7)
.maxstack 8 // return <>2__current;
IL_0000: ldarg.0
IL_0001: ldfld object School.ClassOfStudent/'<GetEnumerator>d__12'::'<>2__current'
IL_0006: ret
} // end of method '<GetEnumerator>d__12'::'System.Collections.Generic.IEnumerator<System.Object>.get_Current' .method private final hidebysig newslot virtual
instance void System.Collections.IEnumerator.Reset () cil managed
{
.custom instance void [System.Runtime]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = (
01 00 00 00
)
.override method instance void [System.Runtime]System.Collections.IEnumerator::Reset()
// Method begins at RVA 0x2415
// Code size 6 (0x6)
.maxstack 8 // throw new NotSupportedException();
IL_0000: newobj instance void [System.Runtime]System.NotSupportedException::.ctor()
IL_0005: throw
} // end of method '<GetEnumerator>d__12'::System.Collections.IEnumerator.Reset .method private final hidebysig specialname newslot virtual
instance object System.Collections.IEnumerator.get_Current () cil managed
{
.custom instance void [System.Runtime]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = (
01 00 00 00
)
.override method instance object [System.Runtime]System.Collections.IEnumerator::get_Current()
// Method begins at RVA 0x241c
// Code size 7 (0x7)
.maxstack 8 // return <>2__current;
IL_0000: ldarg.0
IL_0001: ldfld object School.ClassOfStudent/'<GetEnumerator>d__12'::'<>2__current'
IL_0006: ret
} // end of method '<GetEnumerator>d__12'::System.Collections.IEnumerator.get_Current // Properties
.property instance object 'System.Collections.Generic.IEnumerator<System.Object>.Current'()
{
.get instance object School.ClassOfStudent/'<GetEnumerator>d__12'::'System.Collections.Generic.IEnumerator<System.Object>.get_Current'()
}
.property instance object System.Collections.IEnumerator.Current()
{
.get instance object School.ClassOfStudent/'<GetEnumerator>d__12'::System.Collections.IEnumerator.get_Current()
} } // end of class <GetEnumerator>d__12
yield return Il代码讲解的更多相关文章
- 可惜Java中没有yield return
项目中一个消息推送需求,推送的用户数几百万,用户清单很简单就是一个txt文件,是由hadoop计算出来的.格式大概如下: uid caller 123456 12345678901 789101 12 ...
- 读懂IL代码就这么简单 (一)
一前言 感谢 @冰麟轻武 指出文章的错误之处,现已更正 对于IL代码没了解之前总感觉很神奇,初一看完全不知所云,只听高手们说,了解IL代码你能更加清楚的知道你的代码是如何运行相互调用的,此言一出不明觉 ...
- 读懂IL代码就这么简单
原文地址:http://www.cnblogs.com/zery/p/3366175.html 一前言 感谢 @冰麟轻武 指出文章的错误之处,现已更正 对于IL代码没了解之前总感觉很神奇,初一看完全不 ...
- 读懂IL代码(二)
上一篇提到了最基本的IL代码,应该是比较通俗易懂的,所以有了上一篇的基础之后,这篇便要深入一点点的来讲述了. 首先我必须再来说一些重要的概念: Evaluation Stack(评估栈):这是由.NE ...
- 【转载】读懂IL代码就这么简单 (一)
一前言 感谢 @冰麟轻武 指出文章的错误之处,现已更正 对于IL代码没了解之前总感觉很神奇,初一看完全不知所云,只听高手们说,了解IL代码你能更加清楚的知道你的代码是如何运行相互调用的,此言一出不明觉 ...
- C# yield return 原理探究
天需要些一个小工具,需要使用到多线程读写程序集,接口方法返回值类型需要为"IEnumerable<string>"这里用到了"yield return&quo ...
- C#中的using和yield return混合使用
最近写代码为了为了省事儿用了几个yield return,因为我不想New一个List<T>或者T[]对象再往里放元素,就直接返回IEnumerable<T>了.我的代码里还有 ...
- 读懂IL代码就这么简单(三)完结篇
一 前言 写了两篇关于IL指令相关的文章,分别把值类型与引用类型在 堆与栈上的操作区别详细的写了一遍 这第三篇也是最后一篇,之所以到第三篇就结束了,是因为以我现在的层次,能理解到的都写完了,而且个人认 ...
- 读懂IL代码就这么简单(二)
一 前言 IL系列 第一篇写完后 得到高人指点,及时更正了文章中的错误,也使得我写这篇文章时更加谨慎,自己在了解相关知识点时,也更为细致.个人觉得既然做为文章写出来,就一定要保证比较高的质量,和正确率 ...
随机推荐
- 微服务架构 | 2.2 Alibaba Nacos 的统一配置管理
目录 前言 1. Nacos 配置中心基础知识 1.1 Nacos 在配置中心中的功能 1.2 Nacos 配置管理 Data ID 的构成 1.3 Nacos 配置的回滚机制 1.4 Nacos 配 ...
- Azure Terraform(十)利用 Azure DevOps 的条件语句选择发布环境
一,引言 之前我讲过的所有的案例中,都是将整个Azure Resource 部署到同一个订阅下,没有做到灵活的在 Azure Pipeline 在运行前选择需要部署的环境.在实际的项目开发中,我们也会 ...
- gin中的重定向
package main import ( "github.com/gin-gonic/gin" ) func main() { // HTTP重定向很容易,内部.外部重定向均支持 ...
- Tomcat-IDEA整合Tomcat服务器
Tomcat(IDEA整合Tomcat服务器) 可以加多个版本tomcat
- centos下python2升级为python3
1.下载 下载地址 https://www.python.org/downloads/release/python-353/ 选择"Gzipped source tarball"这 ...
- QPS、TPS、并发用户数、吞吐量
1.QPS QPS Queries Per Second 是每秒查询率 ,是一台服务器 每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内 所处理流量多少的衡量标准, 即每秒的响应请求数,也 ...
- python列表和索引--7
备注:列表元素索引下限从0开始,列表用[ ]表示
- java秒杀系列(1)- 秒杀方案总体思路
前言 首先,要明确一点,高并发场景下系统的瓶颈出现在哪里,其实主要就是数据库,那么就要想办法为数据库做层层防护,减轻数据库的压力. 一.简单图示 我用一个比较简单直观的图来表达大概的处理思路 二.生产 ...
- Spring源码-IOC部分-Bean实例化过程【5】
实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] Spring ...
- AGC005 部分简要题解
E 首先注意到这个问题事实上非常复杂(两棵树上博弈),基础的转化无效,于是考虑简化情形. 手玩很多样例可以发现,似乎很容易出现无限循环的情况,进一步地,有观察: 若第一棵树上存在相邻点 \(u, v\ ...