[ACCEPTED]-getting the name of the calling method-reflection

Accepted answer
Score: 12

You need to instantiate the StackFrame with an appropriate 6 constructor. For example,

Dim stackframe As New Diagnostics.StackFrame(1)

will skip one 5 stack frame (the logging method itself) and 4 get the frame for the caller of that method.

That 3 said - if at all possible, I'd strongly 2 recommend using a standard logging mechanism 1 such as log4net.

Score: 10

I think the above statement can be made 2 into one single line:

MsgBox((New System.Diagnostics.StackTrace).GetFrame(1).GetMethod.Name)

This will display a 1 Message Box as with calling method name.

Score: 4

VB.Net

System.Reflection.MethodBase.GetCurrentMethod.Name()

0

Score: 0

You should be able to get this from the 4 StackFrame with the constructor telling 3 it how many frames to go up the chain, so 2 getting the parent would use 1 (the frame 1 is zero based, so 0 is the current method):

Dim stackTrace as New Diagnostics.StackFrame(1)
Return stackTrace.GetMethod.Name
Score: 0

Do not try using StackFrame. It will work 4 only in debug mode which is a killer for 3 application speed.

There is no way you can 2 get this info in Release mode, you should 1 consider changing design and adding parameter.

Score: 0

I add the calling form as the first parameter 4 for all sub routines and functions like 3 calledfunction(me,para1,para2......) so 2 that i can identify the calling form and 1 use its name or text in the called routine

More Related questions