Get Method Name in C#

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:

Getting the Name of the Current Method

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.

Getting the Assembly of the Calling Method

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.

  1. Retrieve method name in C#

    string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
    
  2. Getting the name of the calling method in C#

    string callingMethodName = new System.Diagnostics.StackTrace(1).GetFrame(0).GetMethod().Name;
    
  3. C# method name reflection

    string methodName = MethodBase.GetCurrentMethod().Name;
    
  4. Dynamically obtain method name in C#

    string methodName = MethodBase.GetCurrentMethod().Name;
    
  5. C# get method name from MethodInfo

    string methodName = myMethodInfo.Name;
    
  6. Get calling method name with StackFrame in C#

    string callingMethodName = new StackFrame(1).GetMethod().Name;
    
  7. C# reflection to get method name

    string methodName = MethodBase.GetCurrentMethod().Name;
    
  8. Identify current method name in C#

    string methodName = MethodBase.GetCurrentMethod().Name;
    
  9. C# get method name from lambda expression

    string methodName = ((MethodCallExpression)((Expression<Action>) (() => YourMethod())).Body).Method.Name;
    
  10. Find method name in C# using CallerMemberName attribute

    void YourMethod([CallerMemberName] string methodName = "")
    {
        // Use methodName
    }
    
  11. C# get method name in a class

    string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
    
  12. Extract method name using Expression in C#

    string methodName = ((MethodCallExpression)((Expression<Action>)(() => YourMethod()).Body)).Method.Name;
    
  13. C# method name as string

    string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
    
  14. Get method name from Action delegate in C#

    Action myAction = YourMethod;
    string methodName = myAction.Method.Name;
    
  15. C# print method name dynamically

    Console.WriteLine("Current method: " + MethodBase.GetCurrentMethod().Name);
    
  16. Display method name in log in C#

    log.Write("Current method: " + MethodBase.GetCurrentMethod().Name);
    
  17. C# log method name and parameters

    void LogMethod(string methodName, params object[] parameters)
    {
        // Log method name and parameters
    }
    
  18. Using CallerInfo attributes to get method name in C#

    void YourMethod([CallerMemberName] string methodName = "")
    {
        // Use methodName
    }
    
  19. C# method name from within a lambda function

    Action myAction = () => { string methodName = MethodBase.GetCurrentMethod().Name; };