C#基礎-獲得當前程序的 空間名.類名.方法名


string typeName = this.GetType().ToString();//空間名.類名

string typeName = this.GetType().Name;//類名

new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name 方法名

 

 

---------------------------------------------------------

 01 //測試日志
  02 protected void writeerror(object sender, EventArgs e)
  03 {
  04 string typeName = this.GetType().ToString();//當類名用
  05 //string methodName = new System.Diagnostics.StackTrace(true).GetFrame(1).GetMethod().DeclaringType.ToString();//這個可以打印出由button調用
  06 //string methodName = new System.Diagnostics.StackTrace(true).GetFrame(1).GetMethod().Name;//事件源,OnClick,但是不顯示writeerror這個方法名。。
  07 string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;//程序執行位置代碼塊的方法名
  08 //Response.Write(typeName + "," + methodName+","+method+",");
  09 string errmsg = "test err message";
  10 log.write(Enums.LogType.Error,errmsg, "TYPE NAME:"+typeName, "METHOD NAME:"+methodName);
  11 }
  直接上代碼,也就是說當前的gettype()能當當前類名用,此外System.Diagnostics.StackTrace和System.Reflection.MethodBase大有文章可挖,去翻MSDN吧,呵呵
  當前頁面為test.aspx.cs
  上述三個methodName的情況下輸出如下,自己挑着用吧
  2010-09-29 16:30:23 test err message
  TYPE NAME:ASP.test_aspx(雖然不是真正的類名,但是用來寫Log是夠了,想反射的話,肯定不行,知道得到真正的類別的話請告訴我,謝謝)
  METHOD NAME:System.Web.UI.WebControls.Button
  2010-09-29 16:30:52 test err message
  TYPE NAME:ASP.test_aspx
  METHOD NAME:OnClick
  2010-09-29 16:31:11 test err message
  TYPE NAME:ASP.test_aspx
  METHOD NAME:writeerror(正是我要的)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM