Possible Duplicate:
How can I find the method that called the current method?
I need a way to know the name of calling methods in C#.
For instance:
private void doSomething() { // I need to know who is calling me? (method1 or method2). // do something pursuant to who is calling you? } private void method1() { doSomething(); } private void method2() { doSomething(); }
When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.
Method Calls A method is a routine that applies to a particular class of objects. Once an object is declared, you can refer to it by its identifier when calling methods. You can call methods for a window that has not previously been declared using a special identifier for dynamic instantiation.
But for functions with arguments, we can call a function in two different ways, based on how we specify the arguments, and these two ways are: Call by Value. Call by Reference.
By default, C programming uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function.
from http://www.csharp-examples.net/reflection-calling-method-name/
using System.Diagnostics; // get call stack StackTrace stackTrace = new StackTrace(); // get calling method name Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With