Why is it not possible to get local variable names using Reflection?

后端 未结 4 2299
走了就别回头了
走了就别回头了 2021-02-20 13:33

If I have a code like this:

public class Program
{
    public static void Main()
    {
        string bar = \"\";
        int foo = 24;
    }
}

4条回答
  •  深忆病人
    2021-02-20 14:25

    I think you're looking at a Debug build. The id part of the .locals declaration is optional - so there's no guarantee that the names are retained.

    See MS Partition II which describes the IL Metadata, section 15.4.1.3 for further details:

    MethodBodyItem ::= …
       .locals [ init ] ‘(’ LocalsSignature ‘)’
    LocalsSignature ::= Local [ ‘,’ Local ]*
    Local ::= Type [ Id ]
    

提交回复
热议问题