c# – 恢复.Net中的本地名称

所以问题是:为什么反编译器不能恢复局部变量的名称?我认为反编译器会删除有关本地名称的所有信息,只需使用ldarg_0等.这就是为什么这段代码:

    private static int Foo()
    {
        int locA = Console.ReadKey().KeyChar;
        int b = Console.ReadKey().KeyChar;
        int c = Console.ReadKey().KeyChar;
        return locA*b * c;
    }

被反编译成这一个:

private static int Foo() // from .Net reflector 8.2
{
    int keyChar = Console.ReadKey().KeyChar;
    int num2 = Console.ReadKey().KeyChar;
    int num3 = Console.ReadKey().KeyChar;
    return ((keyChar * num2) * num3);
}

很明显,直到今天,当我找到一个反编译器ILSpy时,它反编译它:

// ConsoleApplication101.Program

private static int Foo()
{
    int locA = (int)Console.ReadKey().KeyChar;
    int b = (int)Console.ReadKey().KeyChar;
    int c = (int)Console.ReadKey().KeyChar;
    return locA * b * c;
}

所以原始代码 – 在这里! (我知道,我禁止编译器优化我的代码,但我不在乎)

问题是:当编译器在exe中提供时,为什么所有使用的反编译器(reflector,dotPeek等)都没有显示这个非常重要的信息!

.method private hidebysig static int32  Foo() cil managed
{
  // Размер кода:       56 (0x38)
  .maxstack  2
  .locals init ([0] int32 locA,
           [1] int32 b,
           [2] int32 c,
           [3] int32 CS$1$0000,
           [4] valuetype [mscorlib]System.ConsoleKeyInfo CS$0$0001)
  IL_0000:  nop
  IL_0001:  call       valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
  IL_0006:  stloc.s    CS$0$0001
  IL_0008:  ldloca.s   CS$0$0001
  IL_000a:  call       instance char [mscorlib]System.ConsoleKeyInfo::get_KeyChar()
  IL_000f:  stloc.0
  IL_0010:  call       valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
  IL_0015:  stloc.s    CS$0$0001
  IL_0017:  ldloca.s   CS$0$0001
  IL_0019:  call       instance char [mscorlib]System.ConsoleKeyInfo::get_KeyChar()
  IL_001e:  stloc.1
  IL_001f:  call       valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
  IL_0024:  stloc.s    CS$0$0001
  IL_0026:  ldloca.s   CS$0$0001
  IL_0028:  call       instance char [mscorlib]System.ConsoleKeyInfo::get_KeyChar()
  IL_002d:  stloc.2
  IL_002e:  ldloc.0
  IL_002f:  ldloc.1
  IL_0030:  mul
  IL_0031:  ldloc.2
  IL_0032:  mul
  IL_0033:  stloc.3
  IL_0034:  br.s       IL_0036
  IL_0036:  ldloc.3
  IL_0037:  ret
} // end of method Program::Foo

最佳答案 您的观察/假设不正确.该元数据不是exe的一部分. ILSpy和ildasm只能显示原始变量名称,因为这些程序读取了可用的pdb文件.

编译后,您将获得两个文件:exe文件和pdb文件. pdb文件包含元数据(如variablenames,linenumbers).如果您使用能够读取随附的pdb文件的工具打开exe,您将获得与原始源文件更紧密匹配的反编译结果.

要验证自己,可以将pdb文件重命名为其他扩展名,然后在ildasm或ilpsy中打开exe.
ildasm结果是:

// Code size       56 (0x38)
  .maxstack  2
  .locals init (int32 V_0,
           int32 V_1,
           int32 V_2,
           int32 V_3,
           valuetype [mscorlib]System.ConsoleKeyInfo V_4)

存在pdb文件:

 // Code size       56 (0x38)
  .maxstack  2
  .locals init ([0] int32 locA,
           [1] int32 b,
           [2] int32 c,
           [3] int32 CS$1$0000,
           [4] valuetype [mscorlib]System.ConsoleKeyInfo CS$0$0001)

你可以看到差异.从pdb文件中读取本地名称.

要验证您的EXE是否可以找到pdb,您可以使用DUMPBIN命令:

dumpbin /pdbPATH:VERBOSE ConsoleApplication1.exe

这将呈现如下输出:

Dump of file ConsoleApplication1.exe

File Type: EXECUTABLE IMAGE
PDB file ‘C:…\ConsoleApplication1.pdb’ checked. (File not found)
PDB file found at ‘c:…\obj\x86\Debug\ConsoleApplication1.pdb’

John Robbin (Wintellect) PDB Files: What Every Developer Must Know

点赞