MSIL问题(基础)

时间:2009-09-24 18:24:51

标签: c# .net cil il

好吧,我们说这个c#代码:

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

IL:

.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?

2 个答案:

答案 0 :(得分:11)

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

答案 1 :(得分:4)

实例方法具有由this加载的第一个隐式参数ldarg.0