No, only compile-time values, such as constants, can be provided for attributes. This restriction applies to all C # attributes and is not specific to data annotation attributes, but in the case of StringLengthAttribute , it is understood that there is a way to provide different lengths at runtime.
You will need to use another type of validation or create your own attribute that inherits from StringLengthAttribute , which takes Type and the method name for this type as the source for the length value. This approach will be similar to the one used by CustomValidationAttribute , which takes the names ValidatorType and Method as sources for verification.
source share