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系列 第一篇写完后 得到高人指点,及时更正了文章中的错误,也使得我写这篇文章时更加谨慎,自己在了解相关知识点时,也更为细致.个人觉得既然做为文章写出来,就一定要保证比较高的质量,和正确率 ...
随机推荐
- 免费注册香港Apple ID
注册 一.海外Apple ID的好处 1.APP软件资源多,比如传说对决(海外版王者荣耀).海外版抖音等,这些APP软件在国内的apple store是没有的: 2.部分APP软件在海外Apple s ...
- 题解 - 「MLOI」小兔叽
小兔叽 \(\texttt{Link}\) 简单题意 有 \(n\) 个小木桩排成一行,第 \(i\) 个小木桩的高度为 \(h_i\),分数为 \(c_i\). 如果一只小兔叽在第 \(i\) 个小 ...
- pl/sql 编程!
一,pl/sql 是什么? --一.pl/sql 是什么? --1.sql是结构化查询语言. --2.编程语言一般都能够声明变量,写条件判断,循环.sql不具备这些特征,所有sql不是一门编程语言. ...
- Spring源码-IOC部分-Xml Bean解析注册过程【3】
实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] Spring ...
- 计算机网络再次整理————tcp例子第二前奏[四]
前言 前文我们介绍了网络协议的各层,同时也介绍了一下我们在编写代码时候的服务端的accept.bind.listen.connect.send做了什么. 可以说是从宏观的角度,或者代码开发的角度来说的 ...
- Luogu P1438无聊的数列
洛谷 P1438无聊的数列 题目链接 点这里! 题目描述 维护一个数列\(a_i\),支持两种操作: 给出一个长度等于 \(r-l+1\)的等差数列,首项为\(k\) 公差为\(d\) 并将它对应加到 ...
- IDEA使用MybatisCodeHelperPro插件
下载MybatisCodeHelperPro: 链接:https://pan.baidu.com/s/1H6csq9STVh0Ofldh6V6gxQ提取码:r2g3 IDEA安装本地插件: ctrl+ ...
- Media Player播放
转载请注明来源:https://www.cnblogs.com/hookjc/ <object id="player" height="64" width ...
- transition过渡2D、3D效果
过渡(transition)是CSS3中具有颠覆性的特征之一,我们可以在不使用 Flash 动画或 JavaScript 的情况下,当元素从一种样式变换为另一种样式时为元素添加效果. 帧动画:通过一帧 ...
- NSArray文件读写
1.NSArray数据写入到文件中 NSArray *arr = @[@"lnj", @"lmj", @"jjj", @"xcq& ...