Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the original source code location for an assembly

Tags:

c#

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.

like image 762
TugboatCaptain Avatar asked Jan 03 '15 00:01

TugboatCaptain


1 Answers

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.

like image 156
Hans Passant Avatar answered Nov 13 '22 19:11

Hans Passant