C#一些常用方法

1.获取当前程序的空间名、类名、方法名

(1).主体方法Main()

1 class Program
2 {
3     static void Main(string[] args)
4     {
5         Test test = new Test();
6         test.GetName();
7     }
8 
9 }
Main

(2)测试方法Test()

 1 class Test
 2 {
 3     public void GetName()
 4     {
 5         //命名空间.类名(当前代码所在方法的所在类和命名空间)——PingTest.Test
 6         string typeName = this.GetType().ToString();//当类名用
 7         //类名 (当前代码所在方法的所在类和命名空间)——Test
 8         string typeName2 = this.GetType().Name;//类名
 9         //方法名字 (当前代码所在方法名))——GetName
10         string typeName3 = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name;
11         //命名空间.类名(调用该方法的方法的所在类和命名空间)——PingTest.Program
12         string methodName1 = new System.Diagnostics.StackTrace(true).GetFrame(1).GetMethod().DeclaringType.ToString();//这个可以打印出由button调用
13         //调用该方法的方法名——Main()
14         string methodName2 = new System.Diagnostics.StackTrace(true).GetFrame(1).GetMethod().Name;//事件源,OnClick,但是不显示writeerror这个方法名。。
15         
16     }
17 }
Test

 

 

posted @ 2013-05-23 14:13  Kitten Zhang  阅读(163)  评论(0编辑  收藏  举报