Since the only way to set the MaximumLength property is through the attribute constructor, there is no programmatic way to change it after building. You can always use personal reflection to do what you need, provided that you use the code with sufficient trust. If you do not want (or cannot) go along this route, another option would be to replace the viewing models in these conditions, and not just update the attributes as necessary. Then you can simply create the required attribute for each individual view model and not worry about changing the attribute itself programmatically.
One recent option is to write your own StringLength attribute. You can pretty easliy mimic the functionality of the embedded version, since its IsValid() method is very simple:
public override bool IsValid(object value) { int num = (value == null) ? 0 : ((string)value).Length; return value == null || (num >= this.MinimumLength && num <= this.MaximumLength); }
Guiding this, but including the public installer in the MaximumLength property MaximumLength should get what you need.
source share