c# - Reflection - get attribute name and value on property

ID : 10165

viewed : 82

Tags : c#reflectionpropertyinfo

Top 5 Answer for c# - Reflection - get attribute name and value on property

vote vote


Use typeof(Book).GetProperties() to get an array of PropertyInfo instances. Then use GetCustomAttributes() on each PropertyInfo to see if any of them have the Author Attribute type. If they do, you can get the name of the property from the property info and the attribute values from the attribute.

Something along these lines to scan a type for properties that have a specific attribute type and to return data in a dictionary (note that this can be made more dynamic by passing types into the routine):

public static Dictionary<string, string> GetAuthors() {     Dictionary<string, string> _dict = new Dictionary<string, string>();      PropertyInfo[] props = typeof(Book).GetProperties();     foreach (PropertyInfo prop in props)     {         object[] attrs = prop.GetCustomAttributes(true);         foreach (object attr in attrs)         {             AuthorAttribute authAttr = attr as AuthorAttribute;             if (authAttr != null)             {                 string propName = prop.Name;                 string auth = authAttr.Name;                  _dict.Add(propName, auth);             }         }     }      return _dict; } 
vote vote


To get all attributes of a property in a dictionary use this:

typeof(Book)   .GetProperty("Name")   .GetCustomAttributes(false)    .ToDictionary(a => a.GetType().Name, a => a); 

remember to change from false to true if you want to include inheritted attributes as well.

vote vote


If you just want one specific Attribute value For instance Display Attribute you can use the following code:

var pInfo = typeof(Book).GetProperty("Name")                              .GetCustomAttribute<DisplayAttribute>(); var name = pInfo.Name; 
vote vote


I have solved similar problems by writing a Generic Extension Property Attribute Helper:

using System; using System.Linq; using System.Linq.Expressions; using System.Reflection;  public static class AttributeHelper {     public static TValue GetPropertyAttributeValue<T, TOut, TAttribute, TValue>(         Expression<Func<T, TOut>> propertyExpression,          Func<TAttribute, TValue> valueSelector)          where TAttribute : Attribute     {         var expression = (MemberExpression) propertyExpression.Body;         var propertyInfo = (PropertyInfo) expression.Member;         var attr = propertyInfo.GetCustomAttributes(typeof(TAttribute), true).FirstOrDefault() as TAttribute;         return attr != null ? valueSelector(attr) : default(TValue);     } } 


var author = AttributeHelper.GetPropertyAttributeValue<Book, string, AuthorAttribute, string>(prop => prop.Name, attr => attr.Author); // author = "AuthorName" 
vote vote


You can use GetCustomAttributesData() and GetCustomAttributes():

var attributeData = typeof(Book).GetProperty("Name").GetCustomAttributesData(); var attributes = typeof(Book).GetProperty("Name").GetCustomAttributes(false); 

Top 3 video Explaining c# - Reflection - get attribute name and value on property