停課時"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內聯)可與所感知的堆棧幀猴。
嗨,馬克。由於JIT,是否有可能在運行期間改變方法名稱? – Joe 2012-03-14 13:50:08
從here:
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);
但也有一句話,這無法與多線程工作。
http://stackoverflow.com/questions/171970/how-can-i-find-the-method-that-called-the-current-method? – 2008-11-11 09:30:01
此問題是[http://stackoverflow.com/questions/171970/how-can-i-find-the-method-that-called-the-current-method](http://stackoverflow.com/questions/171970/how-can-i-find-the-method-that-called-the-current-method) – 2008-11-11 09:30:59