为什么在MSIL中调用字段之前必须做ldarg.0? [英] Why do I have to do ldarg.0 before calling a field in MSIL?

查看:143
本文介绍了为什么在MSIL中调用字段之前必须做ldarg.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用一个函数,其参数为 string Int32 字符串只是一个文字, Int32 应该是一个字段。所以我认为应该是这样的:

I want to call a function, with as parameters a string and an Int32. The string is just a literal, the Int32 should be a field. So I thought it should be something like:

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

但这会引发错误,即该代码无效。添加时

But this throws the error that this is not valid code. When adding

ldarg.0 

ldfld 之前运行就可以了。为什么会这样,并且在有更多字段时会惹上麻烦?

before ldfld it runs just fine. Why is this, and is this going to get me into trouble when having more fields?

推荐答案

实例方法具有一个称为的隐式参数这个。它被加载为堆栈的第一个参数,因此 this具有ldarg.0。

Instance methods have an implicit parameter called "this". It is loaded as the first argument to the stack, thus you have ldarg.0 for "this".

这篇关于为什么在MSIL中调用字段之前必须做ldarg.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆