JDS JDS - 2 months ago 5x
C# Question

How to read the DisplayAttribute.Name property?

This seems a rather trivial task however I can't see to find how to read the DisplayAttribute.Name property in my code.

I have a class with a property:

[Display(Name="A Nice Field")]
public string SomeField{get;set;}

In my code I am hitting a road block.

object[] attrs = prop.GetCustomAttributes(true);

foreach (Attribute attr in attrs)
if (attr is DisplayAttribute)
// How do I read the attr Name property?

var displayAttribute = (DisplayAttribute)(prop.GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault());