How to get the name of the current method from code [duplicate]
I know you can do
this.GetType().FullName
To get
My.Current.Class
But what can I call to get
My.Current.Class.CurrentMet开发者_开发技巧hod
Call System.Reflection.MethodBase.GetCurrentMethod().Name
from within the method.
using System.Diagnostics;
...
var st = new StackTrace();
var sf = st.GetFrame(0);
var currentMethodName = sf.GetMethod();
Or, if you'd like to have a helper method:
[MethodImpl(MethodImplOptions.NoInlining)]
public string GetCurrentMethod()
{
var st = new StackTrace();
var sf = st.GetFrame(1);
return sf.GetMethod().Name;
}
Updated with credits to @stusmith.
Since C# version 6 you can simply call:
string currentMethodName = nameof(MyMethod);
In C# version 5 and .NET 4.5 you can use the [CallerMemberName] attribute to have the compiler auto-generate the name of the calling method in a string argument. Other useful attributes are [CallerFilePath] to have the compiler generate the source code file path and [CallerLineNumber] to get the line number in the source code file for the statement that made the call.
Before that there were still some more convoluted ways of getting the method name, but much simpler:
void MyMethod() {
string currentMethodName = "MyMethod";
//etc...
}
Albeit that a refactoring probably won't fix it automatically.
If you completely don't care about the (considerable) cost of using Reflection
then this helper method should be useful:
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Reflection;
//...
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetMyMethodName() {
var st = new StackTrace(new StackFrame(1));
return st.GetFrame(0).GetMethod().Name;
}
I think the best way to get the full name is:
this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name;
or try this
string method = string.Format("{0}.{1}", MethodBase.GetCurrentMethod().DeclaringType.FullName, MethodBase.GetCurrentMethod().Name);
Does this not work?
System.Reflection.MethodBase.GetCurrentMethod()
Returns a MethodBase object representing the currently executing method.
Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)
http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getcurrentmethod.aspx
You can also use MethodBase.GetCurrentMethod()
which will inhibit the JIT compiler from inlining the method where it's used.
Update:
This method contains a special enumeration StackCrawlMark
that from my understanding will specify to the JIT compiler that the current method should not be inlined.
This is my interpretation of the comment associated to that enumeration present in SSCLI. The comment follows:
// declaring a local var of this enum type and passing it by ref into a function
// that needs to do a stack crawl will both prevent inlining of the calle and
// pass an ESP point to stack crawl to
//
// Declaring these in EH clauses is illegal;
// they must declared in the main method body
Well System.Reflection.MethodBase.GetCurrentMethod().Name
is not a very good choice 'cause it will just display the method name without additional information.
Like for string MyMethod(string str)
the above property will return just MyMethod
which is hardly adequate.
It is better to use System.Reflection.MethodBase.GetCurrentMethod().ToString()
which will return the entire method signature...
精彩评论