473,394 Members | 1,717 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,394 software developers and data experts.

Programmatically determine method name from within same method.

How can I programmatically determine the [name of a method] from within that
method.

For example, consider the following code:

private void DoSomething()
{
string s = ???;
}

How can I get the string, "DoSomething" into the variable s? (without
hard-coding the method name)?

The reason I want to do this is that I have a centralized error logging
routine in a static class and I call it from error handlers throughout the
application. One of the parameters in the error logging routine is the name
of the method [from which the error logging routine is being called]. I
would like to cut-n-paste the line that calls the static error logging
routine, and I don't want to hard-code the method name into that line of
code. Maybe there is an easier way to go about this than what I'm now
looking to do. I'd prefer to not parse the stack trace (from within the
error logging routine) if there is an easier way.

Thanks!
Dec 6 '06 #1
3 2739
that's easy!

private void DoSomething()
{
//s = "DoSomething"
string s = System.Reflection.MethodInfo.GetCurrentMethod().Na me;
}

Explore the System.Reflection.MethodInfo class for many more details.

Muler.

Merk wrote:
How can I programmatically determine the [name of a method] from within that
method.

For example, consider the following code:

private void DoSomething()
{
string s = ???;
}

How can I get the string, "DoSomething" into the variable s? (without
hard-coding the method name)?

The reason I want to do this is that I have a centralized error logging
routine in a static class and I call it from error handlers throughout the
application. One of the parameters in the error logging routine is the name
of the method [from which the error logging routine is being called]. I
would like to cut-n-paste the line that calls the static error logging
routine, and I don't want to hard-code the method name into that line of
code. Maybe there is an easier way to go about this than what I'm now
looking to do. I'd prefer to not parse the stack trace (from within the
error logging routine) if there is an easier way.

Thanks!
Dec 6 '06 #2

"Merk" <A@B.COMskrev i en meddelelse
news:uz**************@TK2MSFTNGP06.phx.gbl...
How can I programmatically determine the [name of a method] from within
that method.

For example, consider the following code:

private void DoSomething()
{
string s = ???;
}

How can I get the string, "DoSomething" into the variable s? (without
hard-coding the method name)?

The reason I want to do this is that I have a centralized error logging
routine in a static class and I call it from error handlers throughout the
application. One of the parameters in the error logging routine is the
name of the method [from which the error logging routine is being called].
Or you could use log4net for logging. You can configure it to output the
method name from where you call the logger.
Dec 6 '06 #3
In addition,

To find the calling method, use System.Diagnostics.StackTrace and
System.Diagnostics.StackFrame.GetMethod().Name
On Wed, 06 Dec 2006 08:35:43 +0100, muler <mu*************@gmail.com
wrote:
that's easy!

private void DoSomething()
{
//s = "DoSomething"
string s = System.Reflection.MethodInfo.GetCurrentMethod().Na me;
}

Explore the System.Reflection.MethodInfo class for many more details.

Muler.

Merk wrote:
>How can I programmatically determine the [name of a method] from within
that
method.

For example, consider the following code:

private void DoSomething()
{
string s = ???;
}

How can I get the string, "DoSomething" into the variable s? (without
hard-coding the method name)?

The reason I want to do this is that I have a centralized error logging
routine in a static class and I call it from error handlers throughout
the
application. One of the parameters in the error logging routine is the
name
of the method [from which the error logging routine is being called].I
would like to cut-n-paste the line that calls the static error logging
routine, and I don't want to hard-code the method name into that lineof
code. Maybe there is an easier way to go about this than what I'm now
looking to do. I'd prefer to not parse the stack trace (from within the
error logging routine) if there is an easier way.

Thanks!


--
Happy Coding!
Morten Wennevik [C# MVP]
Dec 6 '06 #4

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

Similar topics

4
by: deko | last post by:
I can't move a multi-page report to the last record unless I keep the popup form (that defined it's subreports) open. DoCmd.OpenReport "rptStandard", acViewNormal DoCmd.Close acForm,...
8
by: deko | last post by:
I'm trying to find a way to set form/control properties programmatically. In a nut shell: 1. Open a database 2. Open a form in design view 3. Do something like this: For Each prp In...
3
by: Larry Hunt | last post by:
I am developing a class which will be used in both a WinForm and ASP.NET 2.0 application. The class needs to know which type of application it is running in. How do you programmatically determine...
5
by: Jan Smith | last post by:
I have a C# project that has a tab control with four tab pages. On each tab page is a set of three radio buttons (named radioButton1 through radioButton12). I would like to loop through the radio...
15
by: Oleg Subachev | last post by:
I need to programmatically invoke from other class Click event of the Button on my Form. Button.OnClick method is protected, not public. How to perform this ? Oleg Subachev
1
by: qingxun | last post by:
Hi! Is there a way in VB.NET to determine the caller's class name? System.Reflection.MethodBase.GetCurrentMethod.GetType basically telling me what is my current executing class, but I wanted...
7
by: Dale Sampson | last post by:
As you can tell, I am new to VS.net. I have a VB project with a defined data source pointing to a table in a ..mdb file.-- The associated fields are displayed in textboxes using the...
5
by: ~~~ .NET Ed ~~~ | last post by:
Well the subject says it all but I am going to elaborate a bit. As we all now since 2.0 it is possible to access the Header from code behind without much wizardry and that is a good thing. ...
9
by: kj7ny | last post by:
Is there a way that I can programmatically find the name of a method I have created from within that method? I would like to be able to log a message from within that method (def) and I would like...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.