title: How to iterate through the all Model Display(Name=) attribute values
body: <p>I found that <a href="https://stackoverflow.com/questions/5015830/get-the-value-of-displayname-attribute">code of @RichTebb</a> is great and it returns the Model attribute DisplayName.</p>
  
  <p>But how to iterate through the all Model  Display(Name=) attribute values then?</p>
  
  <p>Thanks for ANY clue!</p>
  
  <p>@RichTebb code</p>
  
  <pre><code>public static class HelperReflectionExtensions
      {
          public static string GetPropertyDisplayString&lt;T&gt;(Expression&lt;Func&lt;T, object&gt;&gt; propertyExpression)
          {
              var memberInfo = GetPropertyInformation(propertyExpression.Body);
              if (memberInfo == null)
              {
                  throw new ArgumentException(
                      "No property reference expression was found.",
                      "propertyExpression");
              }
  
              var displayAttribute = memberInfo.GetAttribute&lt;DisplayAttribute&gt;(false);
  
              if (displayAttribute != null)
              {
                  return displayAttribute.Name;
              }
  // ReSharper disable RedundantIfElseBlock
              else
  // ReSharper restore RedundantIfElseBlock
              {
                  var displayNameAttribute = memberInfo.GetAttribute&lt;DisplayNameAttribute&gt;(false);
                  if (displayNameAttribute != null)
                  {
                      return displayNameAttribute.DisplayName;
                  }
  // ReSharper disable RedundantIfElseBlock
                  else
  // ReSharper restore RedundantIfElseBlock
                  {
                      return memberInfo.Name;
                  }
              }
          }
  
          public static MemberInfo GetPropertyInformation(Expression propertyExpression)
          {
              Debug.Assert(propertyExpression != null, "propertyExpression != null");
              var memberExpr = propertyExpression as MemberExpression;
              if (memberExpr == null)
              {
                  var unaryExpr = propertyExpression as UnaryExpression;
                  if (unaryExpr != null &amp;&amp; unaryExpr.NodeType == ExpressionType.Convert)
                  {
                      memberExpr = unaryExpr.Operand as MemberExpression;
                  }
              }
  
              if (memberExpr != null &amp;&amp; memberExpr.Member.MemberType == MemberTypes.Property)
              {
                  return memberExpr.Member;
              }
  
              return null;
          }
  
          public static T GetAttribute&lt;T&gt;(this MemberInfo member, bool isRequired)
              where T : Attribute
          {
              var attribute = member.GetCustomAttributes(typeof(T), false).SingleOrDefault();
  
              if (attribute == null &amp;&amp; isRequired)
              {
                  throw new ArgumentException(
                      string.Format(
                          CultureInfo.InvariantCulture,
                          "The {0} attribute must be defined on member {1}",
                          typeof(T).Name,
                          member.Name));
              }
  
              return (T)attribute;
          }
  
      }
  </code></pre>
  
  <p><strong>Sample:</strong> </p>
  
  <pre><code>string displayName = ReflectionExtensions.GetPropertyDisplayName&lt;SomeClass&gt;(i =&gt; i.SomeProperty);
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/zVt0G.png" alt="enter image description here"></p>