Why do I have to do ldarg.0 before calling a field in MSIL?

冷暖自知 提交于 2019-12-20 10:25:12

问题


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 

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


回答1:


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



来源:https://stackoverflow.com/questions/1785372/why-do-i-have-to-do-ldarg-0-before-calling-a-field-in-msil

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!