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 m
Why not simply pass the name as constructor parameter? This doesn't hide the dependency, unlike StackFrame/StackTrace.
For example:
public class ApiController
{
private readonly OtherClass _otherClass = new OtherClass(nameof(ApiController));
public void GetMethod()
{
_otherClass.OtherMethod();
}
}
public class OtherClass
{
private readonly string _controllerName;
public OtherClass(string controllerName)
{
_controllerName = controllerName;
}
public void OtherMethod()
{
Console.WriteLine(_controllerName);
}
}