in C# by (9.4k points) edited by
0 like 0 dislike

How can I find out who called a method in C#?

c# who called method
name of calling method c#

1 Answer

0 like 0 dislike
by (9.4k points)

You can get calling method in C# using Reflection mechanism.

To get name of calling method use method StackTrace.GetFrame. Create new instance of StackTrace and call method GetFrame(1). The parameter is index of method call in call stack. Index of the first (the nearest) method call is „1“, so it returns a StackFrame of the calling method (method which directly called the current method). To get the method name use StackFrame.GetMethod (to get MethodBase) and then just get value of Name property.

Following example shows how to get calling method name. This can be useful e.g. to write debugging logs.

using System.Diagnostics;
// get call stack
StackTrace stackTrace = new StackTrace();
// get calling method name

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.