I have a class that with a property annotated with [StringLength(2)]
like the class shown below.
public class Foo
{
public Foo()
{
}
[StringLength(2)]
public string MyProperty { get; set; }
}
Why doesn't the following code to validate the object not throw an error? Shouldn't ValidationException
be thrown?
Foo f = new Foo();
f.MyProperty = "A string longer than 2 characters";
ValidationContext validationContext = new ValidationContext(f);
Validator.ValidateObject(f, validationContext);