This post was most recently updated on December 15th, 2014
Getting the Display Name Attribute Value From a Property
1 2 3 4 5 6 7 8 |
private string GetAttributeDisplayName(PropertyInfo property) { var atts = property.GetCustomAttributes( typeof(DisplayNameAttribute), true); if (atts.Length == 0) return null; return (atts[0] as DisplayNameAttribute).DisplayName; } |
MetadataType Display Names
With generated ORM models Microsoft added another layer of indirectly by creating the metadata class attribute.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[MetadataType(typeof(PersonMetadata))] class Person { [DisplayName("First Name")] public string FirstName { get; set; } public string LastName { get; set; } } public class PersonMetadata { [DisplayName("Last Name")] public string LastName { get; set; } } |
In order to grab the meta data display name, you have to traverse the MetadataType attribute:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
private string GetMetaDisplayName(PropertyInfo property) { var atts = property.DeclaringType.GetCustomAttributes( typeof(MetadataTypeAttribute), true); if (atts.Length == 0) return null; var metaAttr = atts[0] as MetadataTypeAttribute; var metaProperty = metaAttr.MetadataClassType.GetProperty(property.Name); if (metaProperty == null) return null; return GetAttributeDisplayName(metaProperty); } |
Display Name Utility Class
For completeness, here is my display name utility class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Reflection; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace Helpers { public class DisplayNameHelper { public string GetDisplayName(object obj, string propertyName) { if (obj == null) return null; return GetDisplayName(obj.GetType(), propertyName); } public string GetDisplayName(Type type, string propertyName) { var property = type.GetProperty(propertyName); if (property == null) return null; return GetDisplayName(property); } public string GetDisplayName(PropertyInfo property) { var attrName = GetAttributeDisplayName(property); if (!string.IsNullOrEmpty(attrName)) return attrName; var metaName = GetMetaDisplayName(property); if (!string.IsNullOrEmpty(metaName)) return metaName; return property.Name.ToString(); } private string GetAttributeDisplayName(PropertyInfo property) { var atts = property.GetCustomAttributes( typeof(DisplayNameAttribute), true); if (atts.Length == 0) return null; return (atts[0] as DisplayNameAttribute).DisplayName; } private string GetMetaDisplayName(PropertyInfo property) { var atts = property.DeclaringType.GetCustomAttributes( typeof(MetadataTypeAttribute), true); if (atts.Length == 0) return null; var metaAttr = atts[0] as MetadataTypeAttribute; var metaProperty = metaAttr.MetadataClassType.GetProperty(property.Name); if (metaProperty == null) return null; return GetAttributeDisplayName(metaProperty); } } } |