Name of the current function / class

M

Mark Rae

Hi,

Is there any way to return the name of the class and/or function in which
the code is currently executing? E.g. if I had a button called cmdMyButton
on a Form called frmMyForm.cs, could I write something like this in the
button's Click event?

private void cmdMyButton_Click(object sender, System.EventArgs e)
{
string strClassName = <code to return frmMyForm>
string strFunctionName = <code to return cmdMyButton_Click>

MessageBox.Show("Code is currently executing the function " +
strFunctionName + " in the class " + strClassName);
}

Any assistance gratefully received.

Mark Rae
 
A

Andreas Håkansson

Mark,

You can get the name of the current object by calling
this.GetType().Name,
sadly there is no way of getting the name of the currently executing method,
or atleast not as easy. Perhaps you could use Aspect Orientated Programming
(AOP, google this list to find information) but it would add more to your
code
(such as having to derive from ContextBoundObject and ContextBoundAttribute)
then the novalty of knowing the method name.

HTH,

//Andreas
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi Mark,

This is possible, I am not sure to what extent though, through the
System.Diagnostics.StackTrace class.
 
F

fbhcah

Hi Mark,

Use System.Reflection.MethodBase.GetCurrentMethod to get the current
method name.

Use System.Reflection.MethodBase.GetCurrentMethod().DeclaringType to get
the current type.

Hope this helps,
fbhcah
 
N

Not important who I am

After a couple of minutes of looking in the docs:

System.Diagnostics.StackTrace st = new StackTrace();

System.Diagnostics.StackFrame sf = st.GetFrame(0);

System.Reflection.MethodBase mb = sf.GetMethod();

linkLabel1.Text = mb.ReflectedType + " : " + mb.Name;



This will give you the name of the object and the function that is currently
executing.
 
J

Jon Skeet [C# MVP]

You can get the name of the current object by calling
this.GetType().Name,
sadly there is no way of getting the name of the currently executing method,
or atleast not as easy.

Actually, there is:

using System;
using System.Reflection;

public class Test
{
public static void Main()
{
MethodBase currentMethod = MethodBase.GetCurrentMethod();
Console.WriteLine (currentMethod.Name);
}
}
 
A

Andreas Hakansson

Mark,

Yeah, it seems that I jumped the gun on this one. You could infact use
the StackTree class to get the method name.

public void Foo()
{
System.Diagnostics.StackTree st =
new System.Diagnostics.StackTree(true);
string methodName = st.GetMethod().Name;
}

HTH,

//Andreas

Dmitriy Lapshin said:
Hi Mark,

This is possible, I am not sure to what extent though, through the
System.Diagnostics.StackTrace class.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Mark Rae said:
Hi,

Is there any way to return the name of the class and/or function in which
the code is currently executing? E.g. if I had a button called cmdMyButton
on a Form called frmMyForm.cs, could I write something like this in the
button's Click event?

private void cmdMyButton_Click(object sender, System.EventArgs e)
{
string strClassName = <code to return frmMyForm>
string strFunctionName = <code to return cmdMyButton_Click>

MessageBox.Show("Code is currently executing the function " +
strFunctionName + " in the class " + strClassName);
}

Any assistance gratefully received.

Mark Rae
 
M

Mark Rae

Not important who I am said:
After a couple of minutes of looking in the docs:

System.Diagnostics.StackTrace st = new StackTrace();

System.Diagnostics.StackFrame sf = st.GetFrame(0);

System.Reflection.MethodBase mb = sf.GetMethod();

linkLabel1.Text = mb.ReflectedType + " : " + mb.Name;


This will give you the name of the object and the function that is currently
executing.

Utterly brilliant - thanks!

I wonder, could this be extended to, say, reveal the function and class
which has called the current function? E.g. if cmdMyButton_Click in
frmMyForm.cs ran the following code:

private void cmdMyButton_Click(object sender, System.EventArgs e)
{
CMyClass objMyClass = new CMyClass();

bool blnReturn = objMyClass.myFunction();

}

could the myFunction function within the CMyClass class interrogate the
calling function and class?
 
M

Mark Rae

I wonder, could this be extended to, say, reveal the function and class
which has called the current function?

S'OK - I figured it out - just change

System.Diagnostics.StackFrame sf = st.GetFrame(0);

to

System.Diagnostics.StackFrame sf = st.GetFrame(1);

D'uh!
 
N

Not important who I am

Exactly right ; )

Mark Rae said:
S'OK - I figured it out - just change

System.Diagnostics.StackFrame sf = st.GetFrame(0);

to

System.Diagnostics.StackFrame sf = st.GetFrame(1);

D'uh!
 
S

Stu Smith

Be aware that if your function is inlined, the stacktrace method won't
contain the function.

This could lead to a bug that only manifests itself in a release build.

You can mark a method to not inline by applying

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.
MethodImplOptions.NoInlining)]

Hope that helps,
Stu
 
M

Mark Rae

Stu Smith said:
Be aware that if your function is inlined, the stacktrace method won't
contain the function.

This could lead to a bug that only manifests itself in a release build.

You can mark a method to not inline by applying

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.
MethodImplOptions.NoInlining)]

Thanks. Does this code go in the calling function or the function being
called?
 
S

Stu Smith

Mark Rae said:
Stu Smith said:
Be aware that if your function is inlined, the stacktrace method won't
contain the function.

This could lead to a bug that only manifests itself in a release build.

You can mark a method to not inline by applying
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.
MethodImplOptions.NoInlining)]

Thanks. Does this code go in the calling function or the function being
called?

I would assume the callee, but I'm not 100% sure of that.
 

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