Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何在CLR上学习IL_.net_Clr_Il - Fatal编程技术网

.net 如何在CLR上学习IL

.net 如何在CLR上学习IL,.net,clr,il,.net,Clr,Il,由于这些IL代码比我看到的更多,我喜欢学习如何正确解释它们 我找不到像C#编译器之类的文档,所以我想在学习了这些常见的文档之后,我可以很好地处理其余的文档: 下面是一些示例IL代码,其中包含我需要知道的内容: 示例1: .method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 15 (0xf) .maxstack 1 .local

由于这些IL代码比我看到的更多,我喜欢学习如何正确解释它们

我找不到像C#编译器之类的文档,所以我想在学习了这些常见的文档之后,我可以很好地处理其余的文档:

下面是一些示例IL代码,其中包含我需要知道的内容:

示例1:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       15 (0xf)
  .maxstack  1
  .locals init ([0] class EnumReflection.DerivedClass derivedClass)
  IL_0000:  nop
  IL_0001:  newobj     instance void EnumReflection.DerivedClass::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  callvirt   instance void EnumReflection.DerivedClass::WriteOutput()
  IL_000d:  nop
  IL_000e:  ret
} // end of method Program::Main
.method public hidebysig specialname rtspecialname 
        instance void  .ctor() cil managed
{
  // Code size       38 (0x26)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldstr      "Hello"
  IL_0006:  stfld      string EnumReflection.DerivedClass::hello
  IL_000b:  ldarg.0
  IL_000c:  ldstr      "World"
  IL_0011:  stfld      string EnumReflection.DerivedClass::world
  IL_0016:  ldarg.0
  IL_0017:  ldc.i4.s   123
  IL_0019:  stfld      int32 EnumReflection.DerivedClass::age
  IL_001e:  ldarg.0
  IL_001f:  call       instance void EnumReflection.BaseClass::.ctor()
  IL_0024:  nop
  IL_0025:  ret
} // end of method DerivedClass::.ctor
示例2:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       15 (0xf)
  .maxstack  1
  .locals init ([0] class EnumReflection.DerivedClass derivedClass)
  IL_0000:  nop
  IL_0001:  newobj     instance void EnumReflection.DerivedClass::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  callvirt   instance void EnumReflection.DerivedClass::WriteOutput()
  IL_000d:  nop
  IL_000e:  ret
} // end of method Program::Main
.method public hidebysig specialname rtspecialname 
        instance void  .ctor() cil managed
{
  // Code size       38 (0x26)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldstr      "Hello"
  IL_0006:  stfld      string EnumReflection.DerivedClass::hello
  IL_000b:  ldarg.0
  IL_000c:  ldstr      "World"
  IL_0011:  stfld      string EnumReflection.DerivedClass::world
  IL_0016:  ldarg.0
  IL_0017:  ldc.i4.s   123
  IL_0019:  stfld      int32 EnumReflection.DerivedClass::age
  IL_001e:  ldarg.0
  IL_001f:  call       instance void EnumReflection.BaseClass::.ctor()
  IL_0024:  nop
  IL_0025:  ret
} // end of method DerivedClass::.ctor
我知道这些代码是做什么的,因为我制作了它们:-)但是我想了解更多关于相应的IL代码的信息

这些示例包含像,这样的IL代码,您能用问号解释一下该命令吗?这些命令代表什么?这样我们就可以很容易地记住它们。

  • nop(用于调试-无操作)
  • newobj(它似乎正在堆中创建新对象)
  • stloc.0
  • ldloc.0
  • 雷特
  • ldarg.0
  • ldstr
  • stfld
  • ldc.i4.s
  • .ctor-构造函数
理解IL很重要,因为它揭示了特定编译器如何生成代码并在特定情况下执行操作

然而,我找不到一个很好的文档,里面也有关于IL的例子。CLR与C#3.0是一本好书,但最终它不是一本IL书,因此它没有解释IL的所有内容

编辑:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       15 (0xf)
  .maxstack  1
  .locals init ([0] class EnumReflection.DerivedClass derivedClass)
  IL_0000:  nop
  IL_0001:  newobj     instance void EnumReflection.DerivedClass::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  callvirt   instance void EnumReflection.DerivedClass::WriteOutput()
  IL_000d:  nop
  IL_000e:  ret
} // end of method Program::Main
.method public hidebysig specialname rtspecialname 
        instance void  .ctor() cil managed
{
  // Code size       38 (0x26)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldstr      "Hello"
  IL_0006:  stfld      string EnumReflection.DerivedClass::hello
  IL_000b:  ldarg.0
  IL_000c:  ldstr      "World"
  IL_0011:  stfld      string EnumReflection.DerivedClass::world
  IL_0016:  ldarg.0
  IL_0017:  ldc.i4.s   123
  IL_0019:  stfld      int32 EnumReflection.DerivedClass::age
  IL_001e:  ldarg.0
  IL_001f:  call       instance void EnumReflection.BaseClass::.ctor()
  IL_0024:  nop
  IL_0025:  ret
} // end of method DerivedClass::.ctor
我找到了规格,他们告诉我:多亏了@usr

  • nop(用于调试-无操作)
  • newobj-创建一个新对象
  • stloc.0-从堆栈到局部变量的pop值
  • ldloc.0?-将局部变量加载到堆栈上
  • ret-从方法返回
  • ldarg.0-将参数0加载到堆栈中
  • ldstr-加载文本字符串
  • stfld-存储到对象的字段中
  • ldc.i4.s-将num作为int32(缩写形式)推送到堆栈上
  • .ctor-构造函数
。分区III包含关于IL/CIL的丰富信息,适合学习。这是一份优秀的文件


您还可以通过示例学习IL。在C#中编译一些简单的方法,并查看反射器中的IL(它有一个IL模式)。

如果您想要每个操作码的简要概述,您可以做得比检查
System.Reflection.Emit
名称空间更糟糕

例如,有一个类的每个操作码都有一个静态字段。然后,根据堆栈行为,对其中的每一个进行更详细的描述。例如:

ldarg.0指令将索引为0的参数推送到计算堆栈上。ldarg.0指令可用于通过从传入参数复制值类型或原语值,将其加载到堆栈中


有几本书确实涵盖了IL:

  • 请随意编辑此答案并添加一些书籍
还有一些关于IL的章节

另见:

      • 没有-没有行动
      • newobj-创建一个对象并调用其构造函数
      • 0-从堆栈中弹出一个值,并将其存储在第一个局部变量中
      • ldloc.0-将第一个局部变量推送到堆栈上
      • 返回
      • ldarg.0-将第一个参数(
        this
        在实例方法中)推送到堆栈上
      • ldstr-将字符串推送到堆栈上
      • stfld-使用堆栈上的数据设置字段
      • ldc.i4.s-将指定的数字作为整数推送
      • .ctor-构造函数
      我建议您找到有关这些操作码的良好文档来源(尽管Wikipedia可能是最好的:()。System.Reflection.Emit的文档中有相当详细的操作码文档


      最重要的是,构建小程序并检查IL输出。这是最好的学习方法。

      这就是我所做的,但是我也希望了解这些命令的含义。是的,我建议您查看规范。分区III实际上看起来很好,可以理解。我在本节中的规范中找到了我想要的内容pdf文件:ECMA-335的链接是:下载ILSpy并切换它以显示反编译的IL代码。您可以单击所有操作码,ILSpy将引导您查看Microsoft文档。+1@mslot,感谢您在ILSpy中对IL代码的评论,我使用了ILSpy一点,但没有使用IL部分。单击操作码并阅读有关它的文档非常有用。