Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the value of DisplayName attribute

Tags:

c#

attributes

public class Class1 {     [DisplayName("Something To Name")]     public virtual string Name { get; set; } } 

How to get the value of DisplayName attribute in C# ?

like image 755
Bassam Bsata Avatar asked Feb 16 '11 11:02

Bassam Bsata


2 Answers

Try these utility methods of mine:

using System.ComponentModel; using System.Globalization; using System.Linq;   public static T GetAttribute<T>(this MemberInfo member, bool isRequired)     where T : Attribute {     var attribute = member.GetCustomAttributes(typeof(T), false).SingleOrDefault();      if (attribute == null && 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; }  public static string GetPropertyDisplayName<T>(Expression<Func<T, object>> propertyExpression) {     var memberInfo = GetPropertyInformation(propertyExpression.Body);     if (memberInfo == null)     {         throw new ArgumentException(             "No property reference expression was found.",             "propertyExpression");     }      var attr = memberInfo.GetAttribute<DisplayNameAttribute>(false);     if (attr == null)     {         return memberInfo.Name;     }      return attr.DisplayName; }  public static MemberInfo GetPropertyInformation(Expression propertyExpression) {     Debug.Assert(propertyExpression != null, "propertyExpression != null");     MemberExpression memberExpr = propertyExpression as MemberExpression;     if (memberExpr == null)     {         UnaryExpression unaryExpr = propertyExpression as UnaryExpression;         if (unaryExpr != null && unaryExpr.NodeType == ExpressionType.Convert)         {             memberExpr = unaryExpr.Operand as MemberExpression;         }     }      if (memberExpr != null && memberExpr.Member.MemberType == MemberTypes.Property)     {         return memberExpr.Member;     }      return null; } 

Usage would be:

string displayName = ReflectionExtensions.GetPropertyDisplayName<SomeClass>(i => i.SomeProperty); 
like image 192
Rich Tebb Avatar answered Oct 02 '22 21:10

Rich Tebb


First off, you need to get a MemberInfo object that represents that property. You will need to do some form of reflection:

MemberInfo property = typeof(Class1).GetProperty("Name"); 

(I'm using "old-style" reflection, but you can also use an expression tree if you have access to the type at compile-time)

Then you can fetch the attribute and obtain the value of the DisplayName property:

var attribute = property.GetCustomAttributes(typeof(DisplayNameAttribute), true)       .Cast<DisplayNameAttribute>().Single(); string displayName = attribute.DisplayName; 

() parentheses are required typo error

like image 21
R. Martinho Fernandes Avatar answered Oct 02 '22 20:10

R. Martinho Fernandes