views:

74

answers:

5

Hi,

I will try to explain what I need.

Let's say that I have a class like this:

Public Class Example1

    Public Sub ToBeCalled()

        System.Console.WriteLine(
            "The Method or Class or file and its line which called me is: ",
            ?????)

    End Sub

End Class

Then I have a second class like this:

Public Class Second

    Public Sub SomeProcedure()

        Dim obj As Example1 = New Example1

        obj.ToBeCalled()

    End Sub

End Class

Module1
    Dim obj2 As Second = New Second()

    obj2.SomeProcedure()

End Module1

And what I would like to get on the screen instead of "????" is:

1) The Method or Class or file and its line which called me is: Second

2) The Method or Class or file and its line which called me is: SomeProcerure

3) ...... is: Second.vb

4) ...... is: line 54

5)....... is: col 33

Can anybody help me please?

A: 

You're looking for the StackTrace class.

Note that you can only get line numbers in a debug build with a PDB file.

Also note that you should add <MethodImpl(MethodImplOptions.NoInlining)> _ before the method.

SLaks
Why was this downvoted?
SLaks
A: 

In order to get caller (also column and line numbers) you should use System.Diagnostics.StackTrace class.

 StackTrace stackTrace = new StackTrace();
 StackFrame stackFrame = stackTrace.GetFrame(1);
 MethodBase methodBase = stackFrame.GetMethod();
Vitaliy Liptchinsky
Yes I thought about StackTrace class but I don't know which frame, should I use? The issue is that I'm getting the name like: _Lambda$__1:(
truthseeker
0 : _Info1 : _Lambda$__12 : ThreadStart_Context3 : runTryCode4 : ExecuteCodeWithGuaranteedCleanup5 : RunInternal6 : Run7 : ThreadStartI've list all stack frames and there is no method name which called the second one. :( I mean there is direct method in same class but nothing points to another one. :( Is there anybody that will help me?
truthseeker
A: 

The StackTrace class might be able to provide you with the required information.

A simpler way to access this information is currently being considered for VB 11: Lucian's VBlog: Power6: CALLER_MEMBER.

Heinzi
+1  A: 

You can always use the StackTrace class. This will allow you to walk the stack in order to find out information about the methods that lead to your method being called.

In your case, you will want to use the StackFrame class, as you want only one particular frame (you could use the StackTrace class, but it will do an entire trace, and in this case, you just want one frame). With a StackTrace instance, you can call the GetMethod method to obtain the MethodBase instance which represents the method that is making the call.

Here's an example, it's in C#, but easily translatable to VB.NET:

// Skip the current stack frame, get the caller.
StackFrame stackFrame = new StackFrame(1);

// Print out the method name.
Console.WriteLine(stackFrame.GetMethod().Name);

Note that if you wanted additonal information like the line number, etc, etc, you have to provide a debug build with the PDB file.

casperOne
Also, in order to get all the information he is requesting, it needs to be a Debug build with the PDB file present.
Nick
@Nick: Thanks, I've updated my answer to reflect that.
casperOne
A: 

There was no good answer for this question for a very long time, co I'm closing the topic for know. :(

truthseeker