How can I access the DisplayName data annotation value from code?
问题 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: Type type = typeof(Product); DisplayNameAttribute att = (DisplayNameAttribute)type.GetProperty("Name").GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault(); This assumes the attribute