我已经创建了一个自定义属性。
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();它不能满足我的动机
发布于 2014-02-08 12:36:16
这是me.See 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.https://stackoverflow.com/questions/21641913
复制相似问题