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系列 第一篇写完后 得到高人指点,及时更正了文章中的错误,也使得我写这篇文章时更加谨慎,自己在了解相关知识点时,也更为细致.个人觉得既然做为文章写出来,就一定要保证比较高的质量,和正确率 ...
随机推荐
- 集合框架-HashSet集合(无序唯一)
1 package cn.itcast.p4.hashset.demo; 2 3 import java.util.HashSet; 4 import java.util.Iterator; 5 /* ...
- ES_AutoCheck.sh
#!/bin/bash #@es_check #@date 2019/11/26 #@auth tigergao status=`curl -s GET "http://172.16.71. ...
- 【XR-4】文本编辑器
直接做是困难的,不妨依照部分分来思考. - Subtask 3 首先会进入一个误区:维护修改,通过循环串的性质在 \(\tt KMP\) 自动机上优化遍历. 但可以发现这样很难处理,我们不妨 直接维护 ...
- 如何高效地写 Form
工作少不了写"增删改查","增删改查"中的"增"和"改"都与 Form 有关,可以说:提升了 Form 的开发效率,就提 ...
- Java中的增强型for循环
下面是关于增强型for循环对一维数组与二维数组遍历的具体实现: public class NewForLoop { public static void main(String[] args) { i ...
- Android总结【不定期更新】
全屏显示: this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParam ...
- Class.getResource和ClassLoader.getResource的路径写法
Java中取资源时,经常用到Class.getResource和ClassLoader.getResource,这里来看看他们在取资源文件时候的路径问题. Class.getResource(Stri ...
- HDU3315 费用流
为了不让颓影响到学习= = (主要是颓得不想敲代码) 所以,决定在OJ上随便挑一题,能搞便搞,不会就找题解,扒过来,认真研究......(比如这题 原帖:http://m.blog.csdn.net/ ...
- iOS多线程总览 --By 吴帮雷
在iOS中每个进程启动后都会建立一个主线程(UI线程),这个线程是其他线程的父线程.由于在iOS中除了主线程,其他子线程是独立于Cocoa Touch的,所以只有主线程可以更新UI界面(新版iOS中, ...
- docker基础——5.Dockerfile
把应用部署在容器中,改变了用文本文件保存配置信息的方式.而通过传环境变量配置. Dockerfile是一个文本文件,包括容器的指令.按顺序从上到下执行,第一行非注释指令必须是FROM指定基础镜像. D ...