views:

37

answers:

1
public static string ProductHelper(this Product p) {
    // Need to get the DisplayName value for p.Name property
}

EDIT:

[MetadataType(typeof(ProductMetadata))]
public partial class Product {
    public class ProductMetadata {
        [DisplayName("Product name")]
        public object Name { get; set; }
    }
}
+1  A: 
Type type = typeof(Product);
DisplayNameAttribute att = (DisplayNameAttribute)type.GetProperty("Name").GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault();

This assumes the attribute always exists. Modify for the case when it may not.

edit:
To get the value string x = att.DisplayName;

If Product is a base class use Type type = p.GetType(); instead.

jwsample
What if the DisplayName for the Product's Name property is provided through a MetaData class (ProductMetaData)? Please see the edited update.
randomguy
Check the class for that attribute and get a reference to the specified type. Reflect back on the properties in that type instead.
jwsample