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 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
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 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
How can I find the method that called the current method?
Try this:
using System.Diagnostics;
// Get call stack
StackTrace stackTrace = new StackTrace();
// Get calling method name
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);
one-liner:
(new System.Diagnostics.StackTrace()).GetFrame(1).GetMethod().Name
It is from Get Calling Method using Reflection [C#].
Get the name of the current method
System.Reflection.MethodInfo.GetCurrentMethod();
Get the name of the current method being executed
Thread.currentThread().getStackTrace()[1].getMethodName()
Related Topics
Conversion of a Datetime2 Data Type to a Datetime Data Type Results Out-Of-Range Value
How to Bind a List to a Combobox
How to Deal with Files with a Name Longer Than 259 Characters
How to Embed a Text File in a .Net Assembly
Intersection of Multiple Lists with Ienumerable.Intersect()
Change Wpf Controls from a Non-Main Thread Using Dispatcher.Invoke
How to Pass Command-Line Arguments to a Winforms Application
Why Is It Bad to Use an Iteration Variable in a Lambda Expression
C# Splitting Strings on '#' Character
C# Float Expression: Strange Behavior When Casting the Result Float to Int
Generics Open and Closed Constructed Types
ASP.NET MVC: No Parameterless Constructor Defined for This Object
How to Read an Attribute on a Class at Runtime
How to Update Gui with Backgroundworker
Uwp Enable Local Network Loopback
Ignoring a Class Property in Entity Framework 4.1 Code First