2014-02-08 250 views
3

我已經創建了一個自定義屬性。類:屬性屬性

public class DisplayAttribute : Attribute 
    { 
     public bool IsDisplay; 
     public string DisplayName; 

     public DisplayAttribute() 
     { 
      IsDisplay = true; 
      DisplayName = string.Empty; 
     } 

     public DisplayAttribute(bool isDisplay) 
     { 
      IsDisplay = isDisplay; 
      DisplayName = string.Empty; 
     } 

     public DisplayAttribute(string displayName) 
     { 
      IsDisplay = true; 
      DisplayName = displayName; 
     } 

     public DisplayAttribute(bool isDisplay,string displayName) 
     { 
      IsDisplay = isDisplay; 
      DisplayName = displayName; 
     } 
    } 

我創建這個屬性的動機是要限制財產可以上市時,我從一個特定的類

這裏獲取屬性列表是我的課

public class tblContacts : Connection 
    { 
     [Display(false)] 
     public int ContactId { get; set; } 

     [Display(true,"Category Name")] 
     public string CategoryName { get; set; } 

     [Display("First Name")] 
     public string FirstName { get; set; } 
    } 

但下面的語句當我執行我的動機

+0

你有任何異常 –

+0

該方法的GetProperties,甚至不知道你的屬性存在所以它怎麼會知道尋找你的屬性,而忽略的財產?你的代碼中沒有任何東西會告訴GetProperties忽略一個方法,也不會有。你需要做的是在調用GetProperties之後添加你自己的過濾器來放棄你不想要的過濾器。 – jmcilhinney

+0

無論如何我不得不添加註釋來滿足我的動機 –

回答

0

這是我的工作。參見System.Reflection.BindingFlags瞭解更多信息。

using System; 
using System.Reflection; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      Type myType = (typeof(tblContacts)); 
      PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public | BindingFlags.Instance); 
      Console.WriteLine("The number of public properties is {0}.", myPropertyInfo.Length); 

      // Display the public properties. 
      DisplayPropertyInfo(myPropertyInfo); 

      // Get the nonpublic properties. 
      PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance); 
      Console.WriteLine("The number of protected properties is {0}.", myPropertyInfo1.Length); 

      // Display all the nonpublic properties. 
      DisplayPropertyInfo(myPropertyInfo1); 
     } 
     public static void DisplayPropertyInfo(PropertyInfo[] myPropertyInfo) 
     { 

      // Display information for all properties. 
      for (int i = 0; i < myPropertyInfo.Length; i++) 
      { 
       PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i]; 
       Console.WriteLine("The property name is {0}.", myPropInfo.Name); 
       Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType); 
      } 
     } 
    } 

    public class DisplayAttribute : Attribute 
    { 
     public bool IsDisplay; 
     public string DisplayName; 

     public DisplayAttribute() 
     { 
      IsDisplay = true; 
      DisplayName = string.Empty; 
     } 

     public DisplayAttribute(bool isDisplay) 
     { 
      IsDisplay = isDisplay; 
      DisplayName = string.Empty; 
     } 

     public DisplayAttribute(string displayName) 
     { 
      IsDisplay = true; 
      DisplayName = displayName; 
     } 

     public DisplayAttribute(bool isDisplay, string displayName) 
     { 
      IsDisplay = isDisplay; 
      DisplayName = displayName; 
     } 
    } 
    public class tblContacts 
    { 
     [Display(false)] 
     public int ContactId { get; set; } 

     [Display(true, "Category Name")] 
     public string CategoryName { get; set; } 

     [Display("First Name")] 
     public string FirstName { get; set; } 
    } 
} 

OUTPUT:

The number of public properties is 3. 
The property name is ContactId. 
The property type is System.Int32. 
The property name is CategoryName. 
The property type is System.String. 
The property name is FirstName. 
The property type is System.String. 
The number of protected properties is 0. 
+0

所有的屬性將只公開。我不得不過濾顯示只有幾個屬性 –

+0

Type.GetProperties方法(BindingFlags)http://msdn.microsoft.com/en-us/library/kyaxdd3x(v=vs.110).aspx – Elshan