I am developing an MVC 5 application. I want to get the value in [Display(Name = \"\")] attribute in my controller method for any property of any class.
Alex Art's answer almost worked for me. dd.Name simply returned the property name, but dd.GetName() returned the text from the Display attribute.
This should work:
MemberInfo property = typeof(ABC).GetProperty(s);
var dd = property.GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute;
if(dd != null)
{
var name = dd.Name;
}
You can use it:
MemberInfo property = typeof(ABC).GetProperty(s);
var name = property.GetCustomAttribute<DisplayAttribute>()?.Name;