Is it possible to get file information through reflection in C #?

Suppose you have MethodInfo or any other subclass of ClassInfo, in fact, in C #. Is it possible to get the name of the file in which it was declared, and possibly the line numbers where the declaration begins? This information must exist somewhere in the debug mode metadata, since instantiating the StackTrace will provide you with this information. Should I search in System.Diagnostics instead of System.Reflection?

+6
reflection c #
source share
2 answers

Information about line numbers comes from character files (or "program database" .pdb) usually. Tools such as FxCop use a character file to associate IL with source code. I have applied the following API for you: http://msdn.microsoft.com/en-us/library/system.diagnostics.symbolstore.aspx

+4
source share

You must use the Symantor System.Diagnostics classes to extract information from .pdb files. Here is a nice blog on it

Something like this might work:

using System; using System.Diagnostics; class Foo { static void Main() { SmallFunc(); } static void SmallFunc() { PrintStack(); } static void PrintStack() { StackTrace st = new StackTrace(true); // true means get line numbers. foreach(StackFrame f in st.GetFrames()) { Console.Write(f); } } } 
+1
source share

All Articles