C#反射類中所有欄位,屬性,方法(轉)

來源:互聯網
上載者:User

標籤:

可能大家都知道在C#中如何建立一個類,但對於類的結構可能大家不一定瞭解的很清楚,對於我來說,我之前也搞的不是很明白,今天,當我沒事研究反射的時候突然發現了著一點。

我們來看類的結構到底是什麼

    public class People        //類名     {         private static string name;    //欄位              private string sex;//欄位              public string Sex     //屬性         {             get { return sex; }             set { sex = value; }         }              public static string Name    //屬性         {             get { return People.name; }             set { People.name = value; }         }              private People() //建構函式         {              }              public static string GetName()    // 函數         {                  if (string.IsNullOrEmpty(name))             {                 name = "my name";             }             return name;                   }               } 
View Code

 

以上所有的建構函式、事件、欄位、方法和屬性都叫做成員,即Member

運行下列代碼:

    Type t = typeof(People);                  Console.WriteLine("----------------Method------------------");                  MethodInfo[] methods = t.GetMethods( );                 foreach (MethodInfo method in methods)                 {                     Console.WriteLine("Method:" + method);                          //Console.WriteLine(method);                     //Console.WriteLine("傳回值:" + method.ReturnParameter);                 }                 Console.WriteLine("---------------Field-------------------");                 FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance|BindingFlags.Static);                      foreach (FieldInfo field in fields)                 {                     Console.WriteLine("Field:" + field);                 }                      Console.WriteLine("--------------Member--------------------");                 MemberInfo[] members = t.GetMembers();                      foreach (MemberInfo member in members)                 {                     Console.WriteLine("Member:" + member);                 }                 Console.WriteLine("--------------Property--------------------");                 PropertyInfo[] properties = t.GetProperties( );                      foreach (PropertyInfo property in properties)                 {                     Console.WriteLine("Property:" + property);                 }                 Console.WriteLine("--------------Constructor--------------------");                 ConstructorInfo[] constructors = t.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance);                      foreach (ConstructorInfo constructor in constructors)                 {                     Console.WriteLine("Constructor:" + constructor);                 } 
View Code

 

輸出結果為:

 

    ----------------Method------------------     Method:System.String get_Sex()     Method:Void set_Sex(System.String)     Method:System.String get_Name()     Method:Void set_Name(System.String)     Method:System.String GetName()     Method:System.String ToString()     Method:Boolean Equals(System.Object)     Method:Int32 GetHashCode()     Method:System.Type GetType()     ---------------Field-------------------     Field:System.String sex     Field:System.String name     --------------Member--------------------     Member:System.String get_Sex()     Member:Void set_Sex(System.String)     Member:System.String get_Name()     Member:Void set_Name(System.String)     Member:System.String GetName()     Member:System.String ToString()     Member:Boolean Equals(System.Object)     Member:Int32 GetHashCode()     Member:System.Type GetType()     Member:System.String Sex     Member:System.String Name     --------------Property--------------------     Property:System.String Sex     Property:System.String Name     --------------Constructor--------------------     Constructor:Void .ctor()     請按任意鍵繼續. . . 
View Code

 

另外,我們發現屬性Sex和Name編譯以後變成了get_Sex,get_Name,set_Sex,set_Name,呵呵,看來還是更java一樣

原文出處:http://www.cnblogs.com/shuzhengyi/archive/2010/10/12/1848991.html

C#反射類中所有欄位,屬性,方法(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.