Search code examples
c#reflectiontypesfilenames.net-assembly

How to determine the code-file filename from a compiled application at runtime


Let's say I have an application with two files. Console.cs and Business.cs

Console.cs has program Main class.

Business.cs has three classes named Customer, Order and Orderline.

Is there anyway in C# to determine at runtime (maybe with reflection) that the business objects are in a file named Business.cs?


Solution

  • The C# compiler does not emit this information into the DLL, so it's not available through reflection. However, as you'll be aware from debugging, the debugger can match up compiled locations to source code locations. It does this through PDB files. So it might be theoertically possible for you to ship your PDB files, and invoke the unmanaged debugger or diagnostic symbol store API (see General Reference > Unmanaged API Reference in MSDN) to determine where a given method was defined. You can't really do this for a class, though, because a class could be spread across multiple files using partial classes.