How to get the name of the current method from code
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.
Get the name of the current method
System.Reflection.MethodInfo.GetCurrentMethod();
Get the name of the currently executing method in dotnet core
CallerMemberNameAttribute Allows you to obtain the method or property name of the caller to the method.
public void DoProcessing()
{
TraceMessage("Something happened.");
}
public void TraceMessage(string message,
[System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
System.Diagnostics.Trace.WriteLine("message: " + message);
System.Diagnostics.Trace.WriteLine("member name: " + memberName);
System.Diagnostics.Trace.WriteLine("source file path: " + sourceFilePath);
System.Diagnostics.Trace.WriteLine("source line number: " + sourceLineNumber);
}
// Sample Output:
// message: Something happened.
// member name: DoProcessing
// source file path: c:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoCS\CallerInfoCS\Form1.cs
// source line number: 31
Getting the name of the currently executing method
Thread.currentThread().getStackTrace()
will usually contain the method you’re calling it from but there are pitfalls (see Javadoc):
Some virtual machines may, under some circumstances, omit one or more stack frames from the stack trace. In the extreme case, a virtual machine that has no stack trace information concerning this thread is permitted to return a zero-length array from this method.
Can you use reflection to find the name of the currently executing method?
As of .NET 4.5, you can also use [CallerMemberName].
Example: a property setter (to answer part 2):
protected void SetProperty<T>(T value, [CallerMemberName] string property = null)
{
this.propertyValues[property] = value;
OnPropertyChanged(property);
}
public string SomeProperty
{
set { SetProperty(value); }
}
The compiler will supply matching string literals at call sites, so there is basically no performance overhead.
Get Current Method Name
As stated here:
Caller Info values are emitted as literals into the Intermediate Language (IL) at compile time. Unlike the results of the StackTrace property for exceptions, the results aren't affected by obfuscation.
So from your method you could try to call the following method like:
public string GetCaller([System.Runtime.CompilerServices.CallerMemberName] string memberName = "")
{
return memberName;
}
How to get the name of current function?
Try this:
System.Reflection.MethodBase.GetCurrentMethod().Name
Get the name of the current method being executed
Thread.currentThread().getStackTrace()[1].getMethodName()
Get the name of the currently executing method
Even better than my first answer you can use __method__:
class Foo
def test_method
__method__
end
end
This returns a symbol – for example, :test_method
. To return the method name as a string, call __method__.to_s
instead.
Note: This requires Ruby 1.8.7.
Related Topics
Questions About Java'S String Pool
Adding External Library to Artifact Jar in Intellij Idea
How to Implement Rest Token-Based Authentication With Jax-Rs and Jersey
Difference Between Applicationcontext.Xml and Spring-Servlet.Xml in Spring Framework
What Is the 'Instanceof' Operator Used For in Java
What's the Difference Between ".Equals" and "=="
Why Does Java'S Hashcode() in String Use 31 as a Multiplier
Javac Is Not Recognized as an Internal or External Command, Operable Program or Batch File
Getresourceasstream Returns Null
String Is Immutable. What Exactly Is the Meaning
How to Do a Deep Copy of a 2D Array in Java
Fastest Way to Determine If an Integer'S Square Root Is an Integer