6

好吧,假设我们有这个 c# 代码:

public override void Write(XDRDestination destination)
{
    destination.WriteInt(intValue);
    destination.WriteBool(boolValue);
    destination.WriteFixedString(str1, 100);
    destination.WriteVariableString(str2, 100);
}

伊利诺伊:

.method public hidebysig virtual instance void 
        Write(class [XDRFramework]XDRFramework.XDRDestination destination) cil managed
{
  // Code size       53 (0x35)
  .maxstack  8
  IL_0000:  ldarg.1
  IL_0001:  ldarg.0
  IL_0002:  call       instance int32 LearnIL.Test1::get_intValue()
  IL_0007:  callvirt   instance void [XDRFramework]XDRFramework.XDRDestination::WriteInt(int32)
  IL_000c:  ldarg.1
  IL_000d:  ldarg.0
  IL_000e:  call       instance bool LearnIL.Test1::get_boolValue()
  IL_0013:  callvirt   instance void [XDRFramework]XDRFramework.XDRDestination::WriteBool(bool)
  IL_0018:  ldarg.1
  IL_0019:  ldarg.0
  IL_001a:  call       instance string LearnIL.Test1::get_str1()
  IL_001f:  ldc.i4.s   100
  IL_0021:  callvirt   instance void [XDRFramework]XDRFramework.XDRDestination::WriteFixedString(string,
                                                                                                 uint32)
  IL_0026:  ldarg.1
  IL_0027:  ldarg.0
  IL_0028:  call       instance string LearnIL.Test1::get_str2()
  IL_002d:  ldc.i4.s   100
  IL_002f:  callvirt   instance void [XDRFramework]XDRFramework.XDRDestination::WriteVariableString(string,
                                                                                                    uint32)
  IL_0034:  ret
} // end of method Test1::Write

现在对于这个问题,我的理解是 ldarg.# 将提供给方法的参数放在堆栈上,以便我们可以使用它们?但是,当方法只接受一个参数时,为什么要调用 ldarg.1 和 ldarg.0 呢?

4

2 回答 2

13

实例方法有一个隐式参数 ( this),它作为每个实例方法的第一个参数传递。该指令ldarg.0正在加载this到堆栈中。该指令ldarg.1是加载第一个实数(显式)参数。

于 2009-09-24T18:29:39.980 回答
4

实例方法的第一个隐式参数thisldarg.0.

于 2009-09-24T18:28:36.567 回答