代码之家  ›  专栏  ›  技术社区  ›  Jan Jongboom

为什么在调用msil中的字段之前必须执行ldarg.0?

  •  22
  • Jan Jongboom  · 技术社区  · 15 年前

    我想调用一个函数,其参数为 string 和一个 Int32 .这个 一串 只是字面意思, 英特32 应该是 field . 所以我想应该是这样的:

    .method public hidebysig instance string TestVoid() cil managed
    {
        .maxstack 1
        .locals init (
            [0] string CS$1$0000)
        L_0000: nop 
        L_0001: ldstr "myString"
        L_0006: ldfld int32 FirstNamespace.FirstClass::ByteField
        L_000b: call string [Class1]Class1.TestClass::Functie<int32>(string, int32)
        L_0010: ret 
    }
    

    但这会引发一个错误,即这是无效的代码。添加时

    ldarg.0 
    

    之前 ldfld 它运行得很好。为什么会这样?如果有更多的领域,这会给我带来麻烦吗?

    1 回复  |  直到 15 年前
        1
  •  38
  •   Joel Marcey    15 年前

    实例方法有一个名为“this”的隐式参数。它作为堆栈的第一个参数加载,因此“this”有ldarg.0。