Retrieving the Calling Method Name from Within a Method

Retrieving the calling method name from within a method

I don't think it can be done without tracing the stack. However, it's fairly simple to do that:

StackTrace stackTrace = new StackTrace();
MethodBase methodBase = stackTrace.GetFrame(1).GetMethod();
Console.WriteLine(methodBase.Name); // e.g.

However, I think you really have to stop and ask yourself if this is necessary.

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#].

How To Get Name of Calling Method and Executing Method of Lambda Expression

The expression given in the lambda being a delegate, you will have to traverse into its content to get the actual method details. I suggest updating the parameter type for the Helper method (MeasureExecution) to be Expression<Func<T>> instead of Func<T> as an Expression type would allow you to easily examine the contents of a Func. I did try a sample with the below snippet and it worked as per the expectation. I used a static variable in place of your LogDetails to make my life easier in double checking what is being sent.

public static T MeasureExecution<T>(Expression<Func<T>> func, MethodBase sourceMethod)
{
T funcResult;

funcResult = func.Compile()();

try
{
var executingMethod = string.Empty;
var methodExpression = func.Body as MethodCallExpression;

if (methodExpression != null)
{
executingMethod = methodExpression.Method != null ? methodExpression.Method.Name : "Cannot find method details";
}

MethodInformation = string.Format("Method Being Executed: {0}, Executing Source Class: {1}, Executing Source Method: {2}", executingMethod, sourceMethod.ReflectedType.Name, sourceMethod.Name);
}
catch { }

return funcResult;
}

public static string MethodInformation { get; private set; }

And this is the output I get -
Method Being Executed: GetData, Executing Source Class: DataBinder, Executing Source Method: FindData

How do I get the method name from within that method?

return ste[1+depth].getMethodName();

If you change return statement as above, you would get immediate calling method name , of cource depth shoould be zero..

How to get the caller's method name in the called method?

inspect.getframeinfo and other related functions in inspect can help:

>>> import inspect
>>> def f1(): f2()
...
>>> def f2():
... curframe = inspect.currentframe()
... calframe = inspect.getouterframes(curframe, 2)
... print('caller name:', calframe[1][3])
...
>>> f1()
caller name: f1

this introspection is intended to help debugging and development; it's not advisable to rely on it for production-functionality purposes.

C# Get the calling method's, line number, class file and method name, etc, in the receiving method without parsing any variables or using references?

You can achieve this by using Attributes:

FunctionName that called: CallerMemeberNameAttribute

Caller File: CallerFilePathAttribute

Caller Line Number: CallerLineNumberAttribute

Ruby - How can I get a method name within itself?

Here is the code:

For versions >= 1.9:

def funky_method

return __callee__

end

For versions < 1.9:

def funky_method

return __method__

end


Related Topics



Leave a reply



Submit