473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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 5554
* "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: init.php -> set up DB connections and...
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 scripts. this script performs some calculation and then...
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 a project called "P1".
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 assembly (dll) in which the method is executing. we have...
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 System.Reflection.MethodInfo.GetCurrentMethod().Name from within a property I get...
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 method1() I will call void method5(). In method5...
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> 3: If it returns non-zero, open <this_form> ...
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 not that often used approach, is the GetRows()...
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() Method. This week, we will cover the same exact Method...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.