I recently learned how to create localized display names for my model properties using the following article: Simplified Localization for DataAnnotations
And now I'm trying to push it a little further by removing the parameters from the constructor. Value instead
public class User { [Required] [LocalizedDisplayNameAttribute("User_Id")] public int Id { get; set; } [Required] [StringLength(40)] [LocalizedDisplayNameAttribute("User_FirstName")] public string FirstName { get; set; } [Required] [StringLength(40)] [LocalizedDisplayNameAttribute("User_LastName")] public string LastName { get; set; } }
I want to have it
public class User { [Required] [LocalizedDisplayNameAttribute] public int Id { get; set; } [Required] [StringLength(40)] [LocalizedDisplayNameAttribute] public string FirstName { get; set; } [Required] [StringLength(40)] [LocalizedDisplayNameAttribute] public string LastName { get; set; } }
Now the question is how to resolve this class:
public class LocalizedDisplayNameAttribute : DisplayNameAttribute { private PropertyInfo _nameProperty; private Type _resourceType; public LocalizedDisplayNameAttribute(string className, string propertyName) : base(className + (propertyName == null ? "_Class" : ("_" + propertyName))) { } public override string DisplayName { get { return LanguageService.Instance.Translate(base.DisplayName) ?? "**" + base.DisplayName + "**"; } } }
Know my property name without specifying it in the constructor.
source share