I'm validating my business object properties using EntLib's validation attributes. I would like to localize the MessageTemplate text using a resource file, but I get the following compile time message when I do something like MessageTemplate = Resource.MyMessage:
"An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"
Is there a way to use a resource file for this text?
Thanks, Jay
You need to use the MessageTemplateResourceName
and MessageTemplateResourceType
properties instead of the MessageTemplate
property.
Your code would look something like this:
[StringLengthValidator(1, 50,
MessageTemplateResourceName="InvalidLength",
MessageTemplateResourceType=typeof(MyResource))]
public string FirstName
{
get;
set;
}
Note that you can't use the the strongly typed resource class in the attribute (e.g. MyResource.InvalidLength) because the attribute is expecting a constant and the auto-generated class contains a property. (That is the reason for your error message.)
See Using the Message Template Resources for more information.