这可以用c#吗?
void main()
{
Hello();
}
void Hello()
{
// how do you find out the caller is function 'main'?
}
这可以用c#吗?
void main()
{
Hello();
}
void Hello()
{
// how do you find out the caller is function 'main'?
}
Console.WriteLine(new StackFrame(1).GetMethod().Name);
但是,这并不可靠,尤其是优化(例如 JIT 内联)可能会与感知到的堆栈帧发生冲突。
从这里:
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1);
System.Diagnostics.StackFrame sf = st.GetFrame(0);
string msg = sf.GetMethod().DeclaringType.FullName + "." +
sf.GetMethod().Name;
MessageBox.Show( msg );
但也有一个评论,这不能与多线程一起工作。