简体   繁体   English

反编译的C#是否从IL / MSIL反向工程?

[英]Does decompiled C# get reverse engineered from IL/MSIL?

When I open a tool like ILSpy or dotPeek, I have an option of viewing decompiled C# or the IL. 当我打开像ILSpy或dotPeek这样的工具时,我可以选择查看反编译的C#或IL。

When you view the decompiled source, does the decompiler reverse engineer the IL into the decompiled C# equivalent? 当您查看反编译的源时,反编译器是否将IL反向工程为反编译的C#等效文件?

If so then how would a decompiler infer such a case as the IL below? 如果是这样,那么反编译器如何推断出下面的IL这样的情况呢? (do note that this is a trivial example of the first language construct that came to mind which couldn't easily be inferred from IL, as opposed to actual output from a decompiler): (请注意,这是我想到的第一种语言结构的一个简单例子,它不能从IL中推断出来,而不是反编译器的实际输出):

IL_0000: nop                  // Do nothing (No operation)
IL_0001: ldstr "C"            // Push a string object for the literal string
IL_0006: stloc.0              // Pop a value from stack into local variable 0
IL_0007: ret    

Translate to: 翻译成:

public class C {
    public void M() {
        string foo = nameof(C);
    }
}

Yes, the decompilation tools have to reverse engineer from the IL. 是的,反编译工具必须从IL进行逆向工程。

This is why the tools need to be updated for each version of C#/VB, if you want them to decompile newly introduced language features. 这就是为什么需要为每个版本的C#/ VB更新工具,如果您希望它们反编译新引入的语言功能。 Otherwise, if the tool is not updated, it will either give up on trying to decompile it or it will produce highly convoluted code that a maniac could have written in an earlier version of the language but most probably did not. 否则,如果该工具没有更新,它将放弃尝试反编译它或者它将产生高度复杂的代码,疯子本可以用该语言的早期版本编写但很可能没有。

Some decompilation tools will also attempt to locate the actual source code (via PDBs and Source Servers), and if they're successful in this endeavour then you will see the actual source and of course no decompilation effort occurs in this case. 一些反编译工具也会尝试找到实际的源代码(通过PDB和源服务器),如果他们在这项工作中取得成功,那么你将看到实际的源代码,当然在这种情况下不会发生反编译工作。

Sure those tools will try to reverse engineer from MSIL, but speaking of your particular example - it is not possible to reverse engineer your MSIL to 当然,这些工具将尝试从MSIL进行逆向工程,但谈到您的特定示例 - 无法将MSIL逆向工程

public class C {
    public void M() {
        string foo = nameof(C);
    }
}

Without extenal guidance, like symbol (.pdb) files. 没有extenal指导,如符号(.pdb)文件。 Try to compile that statement above in a library, then delete ".pdb" file and put .dll into dotPeek. 尝试在库中编译上面的语句,然后删除“.pdb”文件并将.dll放入dotPeek。 You will see that it will decompile it as expected: 您将看到它将按预期反编译:

public void M()
{
    string foo = "C";
}

Now put .pdb file back and you will see your nameof(C) expression back. 现在放回.pdb文件,你会看到你的nameof(C)表达式。

Now put .pbd file back, but change the location of source code you used to compile that .dll (for example, rename project folder, delete it completely, or just move .dll with .pdb to different machine). 现在放回.pbd文件,但是改变你用来编译.dll的源代码的位置(例如,重命名项目文件夹,完全删除它,或者只是将带有.pdb的.dll移动到不同的机器上)。 Load it again in dotPeek and you will see again string foo = "C"; 再次在dotPeek中加载它,你会再次看到string foo = "C"; . That is because .pdb symbols might contain path to your actual source code (you might see those paths in exception stack traces for example), which is then used by decompiler tools to show it directly without any decompilation. 这是因为.pdb符号可能包含实际源代码的路径(例如,您可能会在异常堆栈跟踪中看到这些路径),然后反编译工具使用它直接显示它而不进行任何反编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM