2009-09-24 145 views
5

好讓說,我們有這樣的C#代碼:MSIL問題(基本)

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?

回答

11

實例方法具有作爲每個實例方法的第一個參數傳遞的隱式參數(this)。指令ldarg.0正在將this加載到堆棧上。指令ldarg.1是加載第一個真實(顯式)參數。

4

實例方法具有由ldarg.0加載的第一個隱式參數this

+0

你能否擴展你的答案?確定第一個是這個,但在這種情況下是第一個ldarg.1或ldarg.0? – Peter 2009-09-24 18:30:19