Programmatically determine method name from within same method.

M

Merk

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!
 
M

muler

that's easy!

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

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

Muler.
 
P

Peter Kirk

Merk said:
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.
 
M

Morten Wennevik

In addition,

To find the calling method, use System.Diagnostics.StackTrace and
System.Diagnostics.StackFrame.GetMethod().Name


that's easy!

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

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

Muler.
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top