472,338 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,338 software developers and data experts.

Retrieving the Name of the Current Executing Method

clr
I like to stamp trace logs with the name of the executing Class and Method.
I can get the Class Name using GetType.Name and I can get a list of every
Method in the class using System.Reflection.MethodInfo objects (code
follows) But is there anyway to retrieve the name of the currently
executing Method? Or is this a lost cause?

(code sample)
Dim sClassName as String = Me.GetType.Name
Dim aMethod() as System.Reflection.MethodInfo =
Me.GetType.GetMethods(BindingFlags.Public or BindingFlags.Instance or
BindingFlags.DeclaredOnly)
Each member of aMethod has a Name property giving me a complete list of all
of the Class Methods.

Thanks
clr
Nov 20 '05 #1
7 5475
* "clr" <crice@noaddressinparticular> scripsit:
I like to stamp trace logs with the name of the executing Class and Method.
I can get the Class Name using GetType.Name and I can get a list of every
Method in the class using System.Reflection.MethodInfo objects (code
follows) But is there anyway to retrieve the name of the currently
executing Method? Or is this a lost cause?


\\\
MsgBox(System.Reflection.MethodBase.GetCurrentMeth od().Name)
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #2
clr,
You can use either the StrackTrace or StackFrame classes, both in the
System.Diagnostics namespace.

Both support skipping frames (current method), so you can encapsulate the
use of StackTrace or StackFrame in its own method, and not see the
encapsulating method.

Hope this helps
Jay

"clr" <crice@noaddressinparticular> wrote in message
news:Ob**************@TK2MSFTNGP09.phx.gbl...
I like to stamp trace logs with the name of the executing Class and Method. I can get the Class Name using GetType.Name and I can get a list of every
Method in the class using System.Reflection.MethodInfo objects (code
follows) But is there anyway to retrieve the name of the currently
executing Method? Or is this a lost cause?

(code sample)
Dim sClassName as String = Me.GetType.Name
Dim aMethod() as System.Reflection.MethodInfo =
Me.GetType.GetMethods(BindingFlags.Public or BindingFlags.Instance or
BindingFlags.DeclaredOnly)
Each member of aMethod has a Name property giving me a complete list of all of the Class Methods.

Thanks
clr

Nov 20 '05 #3
Hi clr, Jay,

Here's a StackTrace version:

Public Function MeAndMyCaller As String
Dim CurrentStack As New System.Diagnostics.StackTrace

Dim Myself As String = CurrentStack.GetFrame(0).GetMethod.Name _
& " "
&CurrentStack.GetFrame(0).GetFileLineNumber

Dim MyCaller As String = CurrentStack.GetFrame(1).GetMethod.Name _
& " "
&CurrentStack.GetFrame(1).GetFileLineNumber

Return "In " & Myself & vbCrLf & "Called by " & MyCaller
End Function

Regards,
Fergus
Nov 20 '05 #4
clr
Herfried, Jay, and Fergus

I situations where both the MethodBase.GetCurrentMethod and Stack Frame
example are helpful.
Your advise is appreciated.
clr
Nov 20 '05 #5
clr
When working with the Exception Stack Trace Object is there a way to extract
the Line Number without parsing the returned string?

clr

"Fergus Cooney" <fi*****@post.com> wrote in message
news:Oa**************@TK2MSFTNGP12.phx.gbl...
Hi clr, Jay,

Here's a StackTrace version:

Public Function MeAndMyCaller As String
Dim CurrentStack As New System.Diagnostics.StackTrace

Dim Myself As String = CurrentStack.GetFrame(0).GetMethod.Name _
& " "
&CurrentStack.GetFrame(0).GetFileLineNumber

Dim MyCaller As String = CurrentStack.GetFrame(1).GetMethod.Name _
& " "
&CurrentStack.GetFrame(1).GetFileLineNumber

Return "In " & Myself & vbCrLf & "Called by " & MyCaller
End Function

Regards,
Fergus

Nov 20 '05 #6
Hi clr,

Given that Exception StackTrace <is> a string means that you <do> have to
parse it for the line number.

Presumably you are getting this information from an Exception in the Catch
of one of the calling methods - and not where the Exception actually occurred?

Regards,
Fergus
Nov 20 '05 #7
* "clr" <crice@noaddressinparticular> scripsit:
When working with the Exception Stack Trace Object is there a way to extract
the Line Number without parsing the returned string?


General information: The line number will only be available when
compiling in debug mode.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sandman | last post by:
Ok, so I have PHP set upp to ato_prepend and auto_append files to every script I run. So if I someone surfs to /index.php, these scripts run: ...
6
by: vishal | last post by:
hi how can i get the file name from which the current executing file is called. i m making a scrript which will be called from many other...
15
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. How can one get the name of the current project and the current class? This is the situation. Suppose there is...
2
by: SLE | last post by:
Hi there, We have a class method which is invoked through Remoting/IIS. Within this class method, we need to get the physical path of the...
1
by: Guido Kraus | last post by:
Hi, I wonder how to get the name of the currently executing property by using reflection in .Net 2.0 If I use...
9
by: | last post by:
Is it possible to get the name of the method that the code is currently executing in so that I can pass it to another method? For example, in void...
1
by: Chris L. | last post by:
Hi, I'm trying to set up a generic mechanism that would allow the following: 1: Run <this_procedure> 2: After that, run <this_review_procedure>...
9
ADezii
by: ADezii | last post by:
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and...
3
ADezii
by: ADezii | last post by:
Last Tip, we demonstrated the technique for retrieving data from a DAO Recordset, and placing it into a 2-dimensional Array using the GetRows()...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.