7

Is there a way to obtain the location of an assembly's original source code location through reflection?

Warning, I'm not looking for the current location of the assembly, rather the location were the source code resided when it was compiled.

e.g.

Given: myAssembly.dll
   c:\program files\myapp\myAssembly.dll <- I'm NOT looking for this location. This is its current location.
   d:\dev\myapp\main.cs <- this is the location I want; the location were the source code resided when it was compiled

I started with this, but I haven't been able find which rabbit hole to go down to get this info so far.

Assembly.GetExecutingAssembly().GetType("myAssembly.Main").<something>

When exceptions occur in .Net you'll often see the name of the class that threw the exception along with the full path to the original source code file. This is what I'm after. I realize that a .pdb symbol file may be required in order to obtain this location.

TugboatCaptain
  • 4,150
  • 3
  • 47
  • 79

1 Answers1

7

That information is stored in the .PDB file. There are two basic flavors of it, the full one that you get from a Debug build and the stripped one that you get by default from the Release build. A stripped .PDB has the file and line number info removed. Pretty specifically to remove details that most companies consider proprietary.

Project + Properties, Build tab, Advanced button, Debug Info sets this. "Full" is the normal Debug build setting, "pdb-only" is the default Release build setting that produces the stripped version.

Reading the .PDB file is supported by the DbgHelp api, the native flavor. The DIA SDK provides a friendlier COM wrapper for it and the usual choice from C#. The CLR uses it too, that's how you can see the file+line in an exception's stack trace.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    My release build pdb:s (pdb-only) are smaller than debug build (full) but still contain both file name and line number. – adrianm Jan 03 '15 at 19:16
  • The information where the original pdb file is stored is in the dll though. – John May 31 '16 at 20:16