tags:

views:

967

answers:

5

Considering that the debug data file is available (PDB) and by using either System.Reflection or another similar framework such as Mono.Cecil, how to retrieve programmatically the source file name and the line number where a type or a member of a type is declared.

For example, let's say you have compiled this file into an assembly:

C:\MyProject\Foo.cs

1:    public class Foo
2:    {
3:       public string SayHello()
4:       {
5:           return "Hello";
6:       }
7:    }

How to do something like:

MethodInfo methodInfo = typeof(Foo).GetMethod("SayHello");
string sourceFileName = methodInfo.GetSourceFile(); // ?? Does not exist!
int sourceLineNumber = methodInfo.GetLineNumber(); // ?? Does not exist!

sourceFileName would contain "C:\MyProject\Foo.cs" and sourceLineNumber be equal to 3.

Update: System.Diagnostics.StackFrame is indeed able to get that information, but only in the scope of current executing call stack. It means that the method must be invoked first. I would like to get the same info, but without invoking the type member.

A: 

i'm not 100% sure, but I think the System.Diagnostics StackTrace is your friend:

http://www.theserverside.net/discussions/thread.tss?thread_id=27535

Michael Stum
Unfortunately, StackTrace provides information about the current calling stack; and not about an arbitrary outside member. I would like to get the information without actually running the method.
Yann Trevin
Ah ok. Now THAT makes the question even more interesting :-)
Michael Stum
+1  A: 

you might find some help with these links:

Getting file and line numbers without deploying the PDB files also found this following post

"Hi Mark,

The following will give you the line number of your code (in the source file):

Dim CurrentStack As System.Diagnostics.StackTrace MsgBox (CurrentStack.GetFrame(0).GetFileLineNumber)

In case you're interested, you can find out about the routine that you're in, as well as all its callers.

Public Function MeAndMyCaller As String Dim CurrentStack As New System.Diagnostics.StackTrace Dim Myself As String = CurrentStack.GetFrame(0).GetMethod.Name Dim MyCaller As String = CurrentStack.GetFrame(1).GetMethod.Name Return "In " & Myself & vbCrLf & "Called by " & MyCaller End Function

This can be very handy if you want a generalised error routine because it can get the name of the caller (which would be where the error occurred).

Regards, Fergus MVP [Windows Start button, Shutdown dialogue] "

Richard
Unfortunately, StackTrace provides information about the current calling stack; and not about an arbitrary outside member. I would like to get the information without actually running the method.
Yann Trevin
A: 

Hi Yann, Did u able to get answer for your question. I am looking for the same.

Thanks,

Ujjwal
Unfortunately no :(
Yann Trevin
It's possible to use the CCI Metadata Project to do that. See my answer for more details.
Yann Trevin
Please use comments for this as the above is clearly not an answer to the question.
Brian Rasmussen
A: 

The code sample at the following URL provides a class that you can easily modify to get the information you're after:

http://blogs.msdn.com/rmbyers/pages/code-sample-stacktrace-with-manual-symbol-lookup.aspx

Dave
Very interesting sample. Thanks.
Yann Trevin
+1  A: 

By using the PDB reader provided by the CCI Metadata Project, it is possible to extract the code location of a given type member. See an example implementation in the source code of the OSS Gallio Project.

Yann Trevin