J&#252;rgen Steinblock - 10 months ago 106
C# Question

# DisplayAttribute - How can I set a default Resource?

I have a poco class Product

public class Product
{
public int Id {get;set;}

[RequiredEx]
public string ProductName {get;set;}
}


the RequiredEx attribute ist just a little wrapper around RequiredAttribute (System.ComponentMode.DataAnnotations so I don't have to specify the Resource type / name every time.

public class RequiredExAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
public RequiredExAttribute()
{
this.ErrorMessageResourceType = typeof(ModelResources);
this.ErrorMessageResourceName = "ErrorMessage.Required";
}
}


Now I want to decorate my property with the Display attribute

[Display(ResourceType=typeof(ModelResources), Name="Product.ProductName")]
public string ProductName { get; set; }


In this case I can't just inherit from DisplayAttribute because it is sealed. Is there any chance for me not to have to type
ResourceType=typeof(ModelResources)
everytime I use this Attribute?

Since the attribute is sealed, and isn't part of a more complex OO model (the base class is just Attribute, and there are no interfaces implemented), I can't see that you have any options available to you here. Basically, I expect the answer is simply: "you will heed to specify the ResourceType` every time".