Instead of string
ErrorMessage
property of the attribute
System.ComponentModel.DataAnnotations.RequiredAttribute
, better use one of two other way of getting error string information: use the property
ErrorMessageResourceName
or
ErrorMessageResourceType
.
I strongly recommend using
ErrorMessageResourceType
:
https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.errormessageresourcetype(v=vs.110).aspx[
^].
Here is the thing: resource name is still a string, and a compiler won't be able to validate this string. For a type, a compiler at least validates that this is a valid type. Unfortunately, it can be some unrelated type, which is the usual problem if attributes. Fundamentally, the problem is that .NET reflection is not a fully-fledged system based on
meta-types. Explanation of meta-type concepts is too complex and would not be entirely appropriate here: you don't have it with CLR, period.
But with
ErrorMessageResourceType
you can well use the important feature of .resx-based resource technology: each .resx resource creates an auto-generated file, and the type is prescribed in this type, you just use whatever you have in this auto-generated file.
—SA