<分区>
作为 "How can I find the method that called the current method?" 的完全重复关闭
是this可以用 c# 吗?
void main()
{
Hello();
}
void Hello()
{
// how do you find out the caller is function 'main'?
}
<分区>
作为 "How can I find the method that called the current method?" 的完全重复关闭
是this可以用 c# 吗?
void main()
{
Hello();
}
void Hello()
{
// how do you find out the caller is function 'main'?
}
最佳答案
Console.WriteLine(new StackFrame(1).GetMethod().Name);
但是,这并不稳健,尤其是因为优化(例如 JIT 内联)可能会影响感知到的堆栈帧。
关于c# - 你如何找到调用者函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/280413/