萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> C# 裡怎樣得到當前執行的函數名,當前代碼行,源代碼文件名

C# 裡怎樣得到當前執行的函數名,當前代碼行,源代碼文件名

  得到函數名:

  System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();

  this.Text = st.GetFrame(0).ToString();

  得到代碼行,源代碼文件名:

  StackTrace st = new StackTrace(new StackFrame(true));

  Console.WriteLine(" Stack trace for current level: {0}", st.ToString());

  StackFrame sf = st.GetFrame(0);

  Console.WriteLine(" File: {0}", sf.GetFileName());

  Console.WriteLine(" Method: {0}", sf.GetMethod().Name);

  Console.WriteLine(" Line Number: {0}", sf.GetFileLineNumber());

 

  Console.WriteLine(" Column Number: {0}", sf.GetFileColumnNumber());

copyright © 萬盛學電腦網 all rights reserved