问题
I know you can do
this.GetType().FullName
To get
My.Current.Class
But what can I call to get
My.Current.Class.CurrentMethod
回答1:
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.
回答2:
Call System.Reflection.MethodBase.GetCurrentMethod().Name from within the method.
回答3:
Reflection has a knack for hiding the forest for the trees. You never have a problem getting the current method name accurately and quickly:
void MyMethod() {
string currentMethodName = "MyMethod";
//etc...
}
Albeit that a refactoring tool 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;
}
Update: C# version 5 and .NET 4.5 have the golden solution to this common need, 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.
Update2: the syntax I proposed at the top of the answer can now be made to work in C# version 6 without a fancy refactoring tool:
string currentMethodName = nameof(MyMethod);
回答4:
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);
回答5:
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
回答6:
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
回答7:
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...
回答8:
Check this out: http://www.codeproject.com/KB/dotnet/MethodName.aspx
来源:https://stackoverflow.com/questions/2652460/how-to-get-the-name-of-the-current-method-from-code