Although less elegant Elisha solution, but it works as well :)
Your attribute:
[AttributeUsage(AttributeTargets.All, AllowMultiple=false)] class RequiredAttribute : System.Attribute { public string Name {get; set; } public RequiredAttribute(string name) { this.Name = name; } public RequiredAttribute() { this.Name = ""; } }
Some classes:
class Class1 { [Required] public string Address1 { get; set; } public string Address2 { get; set; } [Required] public string Address3 { get; set; } }
Using:
Class1 c = new Class1(); RequiredAttribute ra = new RequiredAttribute(); Type class1Type = c.GetType(); PropertyInfo[] propInfoList = class1Type.GetProperties(); foreach (PropertyInfo p in propInfoList) { object[] a = p.GetCustomAttributes(true); foreach (object o in a) { if (o.GetType().Equals(ra.GetType())) { richTextBox1.AppendText(p.Name + " "); } } }
source share