Skip to main content

C# helper class to assist in retrieving the value of "DisplayName" attribute.

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);
        }
    }
}

// -----------------------------------------
// Refactored (February 25, 2022)
// -----------------------------------------

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Helper
{
    /// <summary>
    /// Helper methods for retrieving the value of a <see cref="System.ComponentModel.DisplayNameAttribute" />.
    /// </summary>
    public static class DisplayNameHelper
    {
        public static string GetDisplayName(object obj, string propertyName)
        {
            return obj == null ? null : GetDisplayName(obj.GetType(), propertyName);
        }

        public static string GetDisplayName(Type type, string propertyName)
        {
            var property = type.GetProperty(propertyName);

            return property == null ? null : GetDisplayName(property);
        }

        public static string GetDisplayName(PropertyInfo property)
        {
            var attrName = GetAttributeDisplayName(property);

            if (!string.IsNullOrEmpty(attrName))
            {
                return attrName;
            }

            var metaName = GetMetaDisplayName(property);

            return !string.IsNullOrEmpty(metaName) ? metaName : property.Name;
        }

        private static string GetAttributeDisplayName(PropertyInfo property)
        {
            var attributes = property.GetCustomAttributes(typeof(DisplayNameAttribute), true);

            return attributes.Length == 0
                ? null
                : (attributes[0] as DisplayNameAttribute)?.DisplayName;
        }

        private static string GetMetaDisplayName(PropertyInfo property)
        {
            if (property.DeclaringType != null)
            {
                var attributes = property.DeclaringType.GetCustomAttributes(typeof(MetadataTypeAttribute), true);
                if (attributes.Length == 0)
                {
                    return null;
                }

                if (attributes[0] is MetadataTypeAttribute metaAttr)
                {
                    var metaProperty = metaAttr.MetadataClassType.GetProperty(property.Name);
                    return metaProperty == null
                        ? null
                        : GetAttributeDisplayName(metaProperty);
                }
            }

            return null;
        }
    }
}