By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,294 Members | 2,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,294 IT Pros & Developers. It's quick & easy.

Programmatically determine method name from within same method.

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.