System.ComponentModel.DataAnnotations.MaxAttribute.IsValid C# (CSharp) Метод

IsValid() публичный Метод

Determines whether the specified value of the object is valid.
public IsValid ( object value ) : bool
value object The value of the object to validate.
Результат bool
        public override bool IsValid(object value)
        {
            if (value == null) return true;

            double valueAsDouble;

            var isDouble = double.TryParse(Convert.ToString(value), out valueAsDouble);

            return isDouble && valueAsDouble <= _max;
        }

Usage Example

        public void IsValidDoubleTests()
        {
            const double max = 3.50f;

            var attribute = new MaxAttribute(max);

            Assert.IsTrue(attribute.IsValid(null));  // Optional values are always valid
            Assert.IsTrue(attribute.IsValid(3));
            Assert.IsTrue(attribute.IsValid("3.498"));
            Assert.IsTrue(attribute.IsValid("-5"));
            Assert.IsFalse(attribute.IsValid(3.51));
            Assert.IsFalse(attribute.IsValid("4"));
            Assert.IsFalse(attribute.IsValid("4.5"));
            Assert.IsFalse(attribute.IsValid(100));
            Assert.IsFalse(attribute.IsValid("100.42"));
            Assert.IsFalse(attribute.IsValid("INVALID STRING"));
        }
All Usage Examples Of System.ComponentModel.DataAnnotations.MaxAttribute::IsValid