类:属性属性

时间:2014-02-08 04:31:24

标签: c# oop properties

我创建了一个自定义属性。

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; }
    }

但是当我执行以下语句

tblContacts obj=new tblContacts();
obj.GetType().GetProperties();

它无法满足我的动机

1 个答案:

答案 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; }
    }
}

输出:

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.