首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类:属性属性

类:属性属性
EN

Stack Overflow用户
提问于 2014-02-08 12:31:24
回答 1查看 208关注 0票数 2

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

代码语言:javascript
复制
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;
        }
    }

我创建此属性的目的是为了限制在从特定类获取属性列表时列出属性

这是我的班级

代码语言:javascript
复制
 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; }
    }

但是当我执行下面的语句时

代码语言:javascript
复制
tblContacts obj=new tblContacts();
obj.GetType().GetProperties();

它不能满足我的动机

EN

回答 1

Stack Overflow用户

发布于 2014-02-08 12:36:16

这是me.See System.Reflection.BindingFlags的工作,了解更多信息。

代码语言:javascript
复制
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; }
    }
}

输出:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21641913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档