1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.Reflection; //使用反射必須導(dǎo)入這個命名空間 7 namespace cShap基礎(chǔ)學(xué)習(xí) 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 Student stu = new Student();14 Type t1 = stu.GetType(); //通過GetType方法創(chuàng)建;15 Type t2 = typeof(Student); //通過typeof操作符創(chuàng)建16 //我們來看看Type獲取到了什么東西吧17 Console.WriteLine("Student類型的name是:{0}", t1.Name);18 Console.WriteLine("Student類型命名空間是:{0}", t1.Namespace);19 20 //獲得Student的方法列表21 foreach(var m in t1.GetMethods() )22 {23 Console.WriteLine("方法的名字->{0},返回值->{1}", m.Name, m.ReturnType);24 }25 26 27 }28 }29 30 class Student31 {32 public void SayHello()33 {34 Console.WriteLine("Hello~~");35 }36 37 public void SayHi()38 {39 Console.WriteLine("Hi``");40 }41 public String name;42 private String pwd;43 }44 }運(yùn)行結(jié)果
關(guān)于Type類在MSDN的資料:http://msdn.microsoft.com/zh-cn/library/system.type.aspx《完》新聞熱點(diǎn)
疑難解答
圖片精選