JDS JDS - 1 year ago 161
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?

Answer Source
var displayAttribute = (DisplayAttribute)(prop.GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download