例如我有2个项目:Item1是
string
,Item2是int
。我该如何在C#中为Item1设置最大长度为8个字符,为Item2设置最大长度为5个数字?string
,Item2是int
。我该如何在C#中为Item1设置最大长度为8个字符,为Item2设置最大长度为5个数字?要做到这一点,只需使用StringLengthAttribute和IntegerValidatorAttribute。
class TestCase
{
[StringLength(8, ErrorMessage = "The TestString value cannot exceed 8 characters.")]
[Required(ErrorMessage="Value Required")]
property string TestString;
[IntegerValidator(MaxValue = 99999)]
property int TestInt;
}