【问题标题】:Class: Property Attributes类:属性属性
【发布时间】:2014-03-05 16:49:24
【问题描述】:

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

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();

它无法满足我的动机

【问题讨论】:

  • 你有什么例外
  • GetProperties 方法甚至不知道您的属性存在,那么为什么它会知道查找您的属性并忽略该属性?您的代码中没有任何内容会告诉 GetProperties 忽略一个方法,而且也不可能存在。您需要做的是在调用 GetProperties 后添加自己的过滤器以丢弃您不想要的过滤器。
  • 反正没有我只需要添加注释来满足我的动机

标签: c# oop properties


【解决方案1】:

这对我有用。有关详细信息,请参阅 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.

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 2019-02-02
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 2020-12-06
  • 2023-03-17
相关资源
最近更新 更多