How to decompile pdb to get c # source code?

My scenario: I want to write to the log file the part of the code where the exception occurred (for example, 5 lines before and 5 lines after the line where the exception occurred, or at least the entire code of this method).

My idea is to decompile the pdb file in C # code and from this decompiled file find the method that was excluded in catch catch.

The pbd file exists and my application is built as a debug version. I know that there are tools that allow decompiling through my GUI (e.g. Reflector), but I want to have this functionality from my code.

How to do it?

+7
source share
3 answers

The PDB contains a mapping between MSIL and the original file name / line number. This is most useful when you can go back and look at the original source files, because decompilation usually does not save line numbers (although it could if it also used the PDB file). It, of course, does not restore the source code exactly as it is written, although with symbol names (also stored in the PDB) it is often close.

+3
source

Look at the source code for ILSpy. This is an open source alternative to Reflector.

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

The relevant parts of the code use the MIT license, which is an authorization license.

+2
source

Use reflection to get the source code of the execution method: System.Reflection.MethodBase.GetCurrentMethod().GetMethodBody();

There is a lot of information you can get with the help of MethodBase members: http://msdn.microsoft.com/en-us/library/system.reflection.methodbase_methods.aspx

See also a good example here for MethodBase information when handling exceptions: http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getmethodbody.aspx#Y563

0
source

All Articles