The Labs \ Source Viewer \ SSCLI \ System.Configuration \ TimeSpanValidator

  1. //------------------------------------------------------------------------------
  2. // <copyright file="TimeSpanValidator.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System;
  16. using System.Collections;
  17. using System.IO;
  18. using System.Reflection;
  19. using System.Security.Permissions;
  20. using System.Xml;
  21. using System.Collections.Specialized;
  22. using System.Globalization;
  23. using System.ComponentModel;
  24. using System.Security;
  25. using System.Text;
  26. using System.Text.RegularExpressions;
  27. namespace System.Configuration
  28. {
  29.    
  30.     public class TimeSpanValidator : ConfigurationValidatorBase
  31.     {
  32.        
  33.         private enum ValidationFlags
  34.         {
  35.             None = 0,
  36.             ExclusiveRange = 1
  37.             // If set the value must be outside of the range instead of inside
  38.         }
  39.        
  40.         private ValidationFlags _flags = ValidationFlags.None;
  41.         private TimeSpan _minValue = TimeSpan.MinValue;
  42.         private TimeSpan _maxValue = TimeSpan.MaxValue;
  43.         private long _resolution = 0;
  44.        
  45.         public TimeSpanValidator(TimeSpan minValue, TimeSpan maxValue) : this(minValue, maxValue, false, 0)
  46.         {
  47.         }
  48.        
  49.         public TimeSpanValidator(TimeSpan minValue, TimeSpan maxValue, bool rangeIsExclusive) : this(minValue, maxValue, rangeIsExclusive, 0)
  50.         {
  51.         }
  52.        
  53.         public TimeSpanValidator(TimeSpan minValue, TimeSpan maxValue, bool rangeIsExclusive, long resolutionInSeconds)
  54.         {
  55.             if (resolutionInSeconds < 0) {
  56.                 throw new ArgumentOutOfRangeException("resolutionInSeconds");
  57.             }
  58.            
  59.             if (minValue > maxValue) {
  60.                 throw new ArgumentOutOfRangeException("minValue", SR.GetString(SR.Validator_min_greater_than_max));
  61.             }
  62.            
  63.             _minValue = minValue;
  64.             _maxValue = maxValue;
  65.             _resolution = resolutionInSeconds;
  66.            
  67.             _flags = rangeIsExclusive ? ValidationFlags.ExclusiveRange : ValidationFlags.None;
  68.         }
  69.        
  70.         public override bool CanValidate(Type type)
  71.         {
  72.             return (type == typeof(TimeSpan));
  73.         }
  74.        
  75.         public override void Validate(object value)
  76.         {
  77.             ValidatorUtils.HelperParamValidation(value, typeof(TimeSpan));
  78.            
  79.             ValidatorUtils.ValidateScalar((TimeSpan)value, _minValue, _maxValue, _resolution, _flags == ValidationFlags.ExclusiveRange);
  80.         }
  81.     }
  82. }

Developer Fusion