[AttributeUsage(AttributeTargets.Property)] public class CustomAttribute : Attribute { public string Description { get; set; } public CustomAttribute(string description) { Description = description; } }
public class MyClass { [Custom("My custom description")] public string MyProperty { get; set; } }
var myClass = new MyClass(); var propertyDescriptor = TypeDescriptor.GetProperties(myClass)["MyProperty"]; var customAttribute = propertyDescriptor.ExplicitAttributes().OfTypeThis will output "My custom description" to the console. The System.ComponentModel.PropertyDescriptor.ExplicitAttributes property is part of the System.ComponentModel.TypeDescriptor package library.().FirstOrDefault(); if (customAttribute != null) { Console.WriteLine(customAttribute.Description); }