-1

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?
            }
        }
John Doe
  • 3,053
  • 17
  • 48
  • 75

1 Answers1

0
var displayAttribute = (DisplayAttribute)(prop.GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault());
serhiyb
  • 4,753
  • 2
  • 15
  • 24