C# Method Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
In C#, you can get the name of the current method or the assembly of the calling method using reflection. Here's how to do it:
public void MethodA() { string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; Console.WriteLine("Current method name: " + methodName); }
In this example, MethodA
calls the static GetCurrentMethod
method of the MethodBase
class to get the MethodBase
object representing the current method. The Name
property is then called on the MethodBase
object to get the name of the current method as a string
, which is printed to the console.
public void MethodA() { var stackTrace = new System.Diagnostics.StackTrace(); var callingMethod = stackTrace.GetFrame(1).GetMethod(); var callingAssembly = callingMethod.DeclaringType.Assembly; Console.WriteLine("Calling assembly: " + callingAssembly.FullName); }
In this example, MethodA
calls the StackTrace
constructor to create a new StackTrace
object, which represents the current call stack. The GetFrame
method is then called with a parameter of 1
, which returns the calling method (the method that called the current method) as a MethodBase
object. The DeclaringType
property is then called on the MethodBase
object to get the Type
object representing the class that contains the calling method, and the Assembly
property is called on the Type
object to get the Assembly
object representing the assembly that contains the calling method. The full name of the calling assembly is then printed to the console.
Retrieve method name in C#
string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
Getting the name of the calling method in C#
string callingMethodName = new System.Diagnostics.StackTrace(1).GetFrame(0).GetMethod().Name;
C# method name reflection
string methodName = MethodBase.GetCurrentMethod().Name;
Dynamically obtain method name in C#
string methodName = MethodBase.GetCurrentMethod().Name;
C# get method name from MethodInfo
string methodName = myMethodInfo.Name;
Get calling method name with StackFrame in C#
string callingMethodName = new StackFrame(1).GetMethod().Name;
C# reflection to get method name
string methodName = MethodBase.GetCurrentMethod().Name;
Identify current method name in C#
string methodName = MethodBase.GetCurrentMethod().Name;
C# get method name from lambda expression
string methodName = ((MethodCallExpression)((Expression<Action>) (() => YourMethod())).Body).Method.Name;
Find method name in C# using CallerMemberName attribute
void YourMethod([CallerMemberName] string methodName = "") { // Use methodName }
C# get method name in a class
string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
Extract method name using Expression in C#
string methodName = ((MethodCallExpression)((Expression<Action>)(() => YourMethod()).Body)).Method.Name;
C# method name as string
string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
Get method name from Action delegate in C#
Action myAction = YourMethod; string methodName = myAction.Method.Name;
C# print method name dynamically
Console.WriteLine("Current method: " + MethodBase.GetCurrentMethod().Name);
Display method name in log in C#
log.Write("Current method: " + MethodBase.GetCurrentMethod().Name);
C# log method name and parameters
void LogMethod(string methodName, params object[] parameters) { // Log method name and parameters }
Using CallerInfo attributes to get method name in C#
void YourMethod([CallerMemberName] string methodName = "") { // Use methodName }
C# method name from within a lambda function
Action myAction = () => { string methodName = MethodBase.GetCurrentMethod().Name; };