How to decompile pdb to get C# source code?

13,217

Solution 1

The PDB contains the mapping between MSIL and source filename / line number. This is most useful when you can go back and look at the original source files, because decompilation typically doesn't preserve line numbers (although it could if it also used the PDB file). It certainly doesn't recover the original code exactly as written, although with symbol names (also stored in the PDB) it often comes close.

Solution 2

Look into the source-code of ILSpy. It's an open source alternative for Reflector.

In particular it uses the libraries Mono.Cecil and Mono.Cecil.Pdb. I suspect the latter can help you with what you want to do.

The relevant code parts use the MIT license, which is a permissive license.

Share:
13,217
Bero
Author by

Bero

developer

Updated on June 17, 2022

Comments

  • Bero
    Bero almost 2 years

    My scenario: I want to write in log file part of code where exception has happened (for example 5 lines before and 5 lines after line where exception happened - or at least all the code of that method).

    My idea is to decompile pdb file in C# code and from that decompiled file find a method that went in exception in catch block.

    Pbd file exists and my app is build as debug version. I know that there are tools that allows through its GUI (for example Reflector) to do decompiling but I want to have that functionality from my code.

    How to do it?

  • Bero
    Bero almost 13 years
    GetMethodBody() doesn't allow me to see source code of the method.
  • Bartosz Wójcik
    Bartosz Wójcik about 11 years
    Please avoid ILSpy and use Reflector, you will save yourself a few problems.
  • CodesInChaos
    CodesInChaos about 11 years
    @BartoszWójcik Could you please explain why using Mono.Cecil.Pdb isn't a good choice?
  • Bartosz Wójcik
    Bartosz Wójcik about 11 years
    The same reason de4dot abandoned Mono.Cecil for .NET manipulations
  • CodesInChaos
    CodesInChaos about 11 years
    @BartoszWójcik How would you embed Reflector into your application to generate extended debug information? Does its license even allow that?