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
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.
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.
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.
Get the name of the current method being executed
Thread.currentThread().getStackTrace()[1].getMethodName()
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
Related Topics
Can't Install Ruby Under Lion With Rvm - Gcc Issues
How to Get a Random Number in Ruby
Oo Design in Rails: Where to Put Stuff
Pg::Error: Select Distinct, Order by Expressions Must Appear in Select List
Avoiding Applescript Through Ruby: Rb-Appscript or Rubyosa
Altering the Primary Key in Rails to Be a String
Error Installing Libv8: Error: Failed to Build Gem Native Extension
Ruby/Ruby on Rails Memory Leak Detection
How to Find the Local Port a Rails Instance Is Running On
Is Ruby Pass by Reference or by Value
Rvm Installation Not Working: "Rvm Is Not a Function"
How to Get Argument Names Using Reflection
Gem Installation Error: You Have to Install Development Tools First (Windows)