gpt4 book ai didi

c# - 类 : Property Attributes

转载 作者:太空宇宙 更新时间:2023-11-03 16:00:34 29 4
gpt4 key购买 nike

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

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

它不能满足我的动机

最佳答案

这对我有用。参见 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.

关于c# - 类 : Property Attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21641913/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com