Getting Class and Method name at runtime

G

Guest

Is there a way to get the name of the class and method in code when not
running in debugger.

For example:
class myclass {
const string modulename = "myclass";

public void mymethod()
{
const string methodname = "mymethod";

string output = "You are in " + modulename + "." + methodname;
}
}

Is there a way to determine the class and method without having to hard code
them?

I use this functionality when reporting messages to the database so that I
can determine where something occurred.

Thank you for any help.

Gregory McCallum
(e-mail address removed)
 
J

Jon Skeet [C# MVP]

gmccallum said:
Is there a way to get the name of the class and method in code when not
running in debugger.

See MethodBase.GetCurrentMethod. You may want to experiment with
exactly which class you want to log, eg if you're in a method declared
in A, overridden in B, and your actual instance is an instance of C...

Jon
 
G

Guest

Perfect, thank you.

Jon Skeet said:
See MethodBase.GetCurrentMethod. You may want to experiment with
exactly which class you want to log, eg if you're in a method declared
in A, overridden in B, and your actual instance is an instance of C...

Jon
 
G

Guest

See MethodBase.GetCurrentMethod. You may want to experiment with
exactly which class you want to log, eg if you're in a method declared
in A, overridden in B, and your actual instance is an instance of C...

Jon
 
G

Guest

I found the answer, so I thought I would share it with others.
This function will return the name of the class and method of the calling
function.

using System.Diagnostics;

private static string[] internGetModuleAndProc ( int stackFramesToSkip )
{
string[] ret = new string[2];
if ( stackFramesToSkip<0 )
stackFramesToSkip=0;
// Skipping 1 to leave out the call to this method
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace (
1+stackFramesToSkip, true );
System.Diagnostics.StackFrame[] sf = st.GetFrames ();
MethodBase mb = sf[0].GetMethod ();
ret[0] = mb.DeclaringType.Name;
ret[1] = mb.Name;
return ret;
}

public void Display_Now ( )
{
// This would set s5 = "THISCLASS.Display_Now"
string s5 = General.GetModuleAndProc ( 0 );
}
 
M

Marcus Andrén

I found the answer, so I thought I would share it with others.
This function will return the name of the class and method of the calling
function.

public void Display_Now ( )
{
// This would set s5 = "THISCLASS.Display_Now"
string s5 = General.GetModuleAndProc ( 0 );
}

Be aware that this solution has a limitation. If the Display_Now
method gets inlined, the string s5 will contain the parent method and
not Display_Now.

The following code should print False if run in the debugger and True
if run outside of it.

public static bool Test()
{
return internGetModuleAndProc(0)[1] == "Main";
}

static void Main()
{
Console.WriteLine("Inlined: " + Test().ToString());
Console.ReadLine();
}
 

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