public void CanValidate() { TimeSpan t = new TimeSpan(1000); TimeSpanValidator v = new TimeSpanValidator(t, t); Assert.True(v.CanValidate(typeof(TimeSpan)), "A1"); Assert.False(v.CanValidate(typeof(int)), "A2"); Assert.False(v.CanValidate(typeof(long)), "A3"); }
static void Main(string[] args) { // Display title. Console.WriteLine("ASP.NET Validators"); Console.WriteLine(); // Create TimeSpan and Validator. TimeSpan testTimeSpan = new TimeSpan(0, 1, 05); TimeSpan minTimeSpan = new TimeSpan(0, 1, 0); TimeSpan maxTimeSpan = new TimeSpan(0, 1, 10); TimeSpanValidator myTimeSpanValidator = new TimeSpanValidator(minTimeSpan, maxTimeSpan, false, 65); // Determine if the object to validate can be validated. Console.WriteLine("CanValidate: {0}", myTimeSpanValidator.CanValidate(testTimeSpan.GetType())); try { // Attempt validation. myTimeSpanValidator.Validate(testTimeSpan); Console.WriteLine("Validated."); } catch (ArgumentException e) { // Validation failed. Console.WriteLine("Error: {0}", e.Message.ToString()); } // Display and wait Console.ReadLine(); }