Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the name of the class which contains the method which called the current method?

Tags:

c#

I have a requirement where I need to know the name of the class (ApiController) which has a method (GetMethod) which is called by another method (OtherMethod) from a different class (OtherClass).

To help explain this, I hope the below pseudo-code snippets help.

ApiController.cs

public class ApiController
{
    public void GetMethod()
    {
        OtherMethod();
    }
}

OtherClass.cs

public class OtherClass()
{
    public void OtherMethod()
    {
        Console.WriteLine(/*I want to get the value 'ApiController' to print out*/)
    }
}

What I've tried:

  • I've looked at How can I find the method that called the current method? and the answers will get me the calling method (OtherMethod) but not the class (ApiController) which has that method
  • I tried [CallerMemberName] and using StackTrace properties but these don't get me the method's class name
like image 701
Troy Poulter Avatar asked Jul 31 '18 05:07

Troy Poulter


People also ask

How do you find the current method name?

The current method name that contains the execution point that is represented by the current stack trace element is provided by the java. lang. StackTraceElement. getMethodName() method.

What is a method name in Java?

A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. Think of a method as a subprogram that acts on data and often returns a value. Each method has its own name.


2 Answers

using System.Diagnostics;

var className = new StackFrame(1).GetMethod().DeclaringType.Name;

Goes to the previous level of the Stack, finds the method, and gets the type from the method. This avoids you needing to create a full StackTrace, which is expensive.

You could use FullName if you want the fully qualified class name.

Edit: fringe cases (to highlight the issues raised in comments below)

  1. If compilation optimizations are enabled, the calling method may be inlined, so you may not get the value you expect. (Credit: Johnbot)
  2. async methods get compiled into a state machine, so again, you may not get what you expect. (Credit: Phil K)
like image 58
Richardissimo Avatar answered Oct 19 '22 19:10

Richardissimo


So it can be done like this,

new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().DeclaringType.Name

StackFrame represents a method on the call stack, the index 1 gives you the frame that contains the immediate caller of the currently executed method, which is ApiController.GetMethod() in this example.

Now you have the frame, then you retrieve the MethodInfo of that frame by calling StackFrame.GetMethod(), and then you use the DeclaringType property of the MethodInfo to get the type in which the method is defined, which is ApiController.

like image 12
kennyzx Avatar answered Oct 19 '22 21:10

kennyzx