To get type properties, we will use:
Type classType = typeof(TestClass);
PropertyInfo[] properties = classType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
To get the attributes defined from the class, we will use:
Type classType = typeof(TestClass);
object[] attributes = classType.GetCustomAttributes(false);
The Boolean flag is passed the Inheritance flag whether to search in the inheritance chain or not.
To get property attributes, we will use:
propertyInfo.GetCustomAttributes(false);
Using the above Harvard code:
Type classType = typeof(TestClass);
object[] classAttributes = classType.GetCustomAttributes(false);
foreach(PropertyInfo property in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
object[] propertyAttributes = property.GetCustomAttributes(false);
Console.WriteLine(property.Name);
}
source
share