C#遍曆類的屬性 PropertyInfo.Attributes

來源:互聯網
上載者:User

PropertyInfo.Attributes 屬性

此屬性工作表示與成員關聯的特性。 所有成員都具有相對於特定成員類型定義的特性集。 屬性特性使使用者能夠知道此屬性是否是預設屬性、SpecialName 屬性等等。

若要擷取 Attributes 屬性,請先擷取類類型。 從 Type 擷取 PropertyInfo。 從 PropertyInfo 擷取特性。

官方樣本:擷取類的特性

 1 using System; 2 using System.Reflection; 3  4 public class Myproperty 5 { 6     private string caption = "Default caption"; 7     public string Caption 8     { 9         get{return caption;}10         set {if(caption!=value) {caption = value;}11         }12     }13 }14 15 class Mypropertyinfo16 {17     public static int Main(string[] args)18     {19         Console.WriteLine("\nReflection.PropertyInfo");20 21         // Define a property.22         Myproperty Myproperty = new Myproperty();23         Console.Write("\nMyproperty.Caption = " + Myproperty.Caption);24 25         // Get the type and PropertyInfo.26         Type MyType = Type.GetType("Myproperty");27         PropertyInfo Mypropertyinfo = MyType.GetProperty("Caption");28 29         // Get and display the attributes property.30         PropertyAttributes Myattributes = Mypropertyinfo.Attributes;31 32         Console.Write("\nPropertyAttributes - " + Myattributes.ToString());33 34         return 0;35     }36 }

官方參考:http://msdn.microsoft.com/zh-cn/library/system.reflection.propertyinfo.attributes

一個例子: 注意:貌似對欄位無效

先建一個類User

 1 namespace ClassLibrary1 2 { 3     public class User     4     {        5         private int userid = 1; 6         public int Userid 7         {  8             get { return userid; } 9             set { userid = value; }10         }  11         private string userName = "jghg";       12         public string UserName{13             get { return userName; } 14             set { userName = value; }15         }16         private string address = "ghjghj";17         public string Address{ 18             get { return address; } 19             set { address = value; }20         }        21         private string email = "jhgjhg";22         public string Email{23             get { return email; }24             set { email = value; }25         }26         private string phone = "ghjgjg";27         public string Phone28         { 29             get { return phone; }30             set { phone = value; }31         }32     }33 }

接著在主程式中擷取類的屬性,看代碼

 1 namespace ConsoleApplication2 {  2     class Program {  3         static void Main(string[] args)  4         {  5             Type type = typeof(ClassLibrary1.User);  6             object obj = Activator.CreateInstance(type);  7             PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);  8             foreach (PropertyInfo p in props) {  9                 Console.WriteLine(p.Name); 10             } 11             Console.ReadLine(); 12         }13     } 14 }

需要引入命名空間:

using System.Reflection;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.