473,837 Members | 1,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Reflecti on.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.Reflecti on.MethodInfo =
Me.GetType.GetM ethods(BindingF lags.Public or BindingFlags.In stance or
BindingFlags.De claredOnly)
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 5580
* "clr" <crice@noaddres sinparticular> 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.Reflecti on.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.R eflection.Metho dBase.GetCurren tMethod().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.Diagnost ics 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@noaddres sinparticular> wrote in message
news:Ob******** ******@TK2MSFTN GP09.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.Reflecti on.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.Reflecti on.MethodInfo =
Me.GetType.GetM ethods(BindingF lags.Public or BindingFlags.In stance or
BindingFlags.De claredOnly)
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.Diagnost ics.StackTrace

Dim Myself As String = CurrentStack.Ge tFrame(0).GetMe thod.Name _
& " "
&CurrentStack.G etFrame(0).GetF ileLineNumber

Dim MyCaller As String = CurrentStack.Ge tFrame(1).GetMe thod.Name _
& " "
&CurrentStack.G etFrame(1).GetF ileLineNumber

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.GetC urrentMethod 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.c om> wrote in message
news:Oa******** ******@TK2MSFTN GP12.phx.gbl...
Hi clr, Jay,

Here's a StackTrace version:

Public Function MeAndMyCaller As String
Dim CurrentStack As New System.Diagnost ics.StackTrace

Dim Myself As String = CurrentStack.Ge tFrame(0).GetMe thod.Name _
& " "
&CurrentStack.G etFrame(0).GetF ileLineNumber

Dim MyCaller As String = CurrentStack.Ge tFrame(1).GetMe thod.Name _
& " "
&CurrentStack.G etFrame(1).GetF ileLineNumber

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@noaddres sinparticular> 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
4032
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 stuff. Buffers output index.php -> The current page with it's layout, output is buffered. postprocess.php -> Fetches buffer, applies layout to page. Now, sometimes I want to break the execution of index.php (or whatever page that is...
6
4069
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 returns the result to the script who asked for. so i want to know the script name of caller script in the current executing script thxs for your help in advance.
15
1996
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
9198
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 tried: 1) System.Reflection.Assembly.GetExecutingAssembly().Location.ToString() returns a temporary path, i.e. c:\windows\microsoft.net\framework\v1.1.4322\temporary asp.net...
1
8039
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 something like "set_MyProperty" instead of "MyProperty". Is there a way to get the current property name without the "set_" or "get_" prefix?
9
26281
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 I want to know which method called me? Because there are many methods calling many other methods I really want a trace of code execution from method to method. Know what I mean? any advice would be greatly appreciated.
1
1862
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> The idea is that the first procedure would have to update records. After that, the review procedure would check if there are unupdated records. If there are, a form would open to show the problem records,
9
35598
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() Method of the Recordset Object. This Method varies slightly from DAO to ADO, so for purposes of this discussion, we'll be talking about DAO Recordsets. The ADO approach will be addressed in the following Tip. We'll be using a Query, consisting of 5...
3
43556
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 (GetRows()), but only as it applies to an ADO Recordset. Although there are similarities in the 2 methodologies, the ADO Method offers 2 more Optional Arguments, is a little more complex, and of course, the syntax is different in creating the...
0
10562
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10265
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7803
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6996
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5667
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4469
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4036
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3122
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.