This question already has answers here:
How can I find the method that called the current method?

(19个回答)


7年前关闭。






我需要一种方法来了解C#中的调用方法的名称。

例如:
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();
}

最佳答案

来自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);

关于c# - 我如何获得C#中的调用方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/394850/

10-11 19:59