C#基础-获得当前程序的 空间名.类名.方法名

来源:互联网 发布:淘宝被同行刷直接访问 编辑:程序博客网 时间:2024/05/19 05:04
C#基础-获得当前程序的 空间名.类名.方法名

string typeName = this.GetType().ToString();//空间名.类名

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

string methodName = 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(正是我要的)


转载;http://www.cnblogs.com/Asa-Zhu/archive/2013/01/18/2866652.html

0 0