Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can get the calling methods in C# [duplicate]

Tags:

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(); } 
like image 555
ecleel Avatar asked Dec 27 '08 10:12

ecleel


People also ask

What is the calling function in C?

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.

What are the method of calling function?

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.

How many ways can C call function?

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.

What method is default used by C to call a method?

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.


1 Answers

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); 
like image 131
Andrew Robertson Avatar answered Nov 02 '22 23:11

Andrew Robertson