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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="IntegerValidator.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.     //
  31.     // Type validators: Unsealed so users can derive to extend the funcionality without repeating it
  32.     // =============================================================================================
  33.     public class IntegerValidator : ConfigurationValidatorBase
  34.     {
  35.         private enum ValidationFlags
  36.         {
  37.             None = 0,
  38.             ExclusiveRange = 1
  39.             // If set the value must be outside of the range instead of inside
  40.         }
  41.        
  42.         private ValidationFlags _flags = ValidationFlags.None;
  43.         private int _minValue = int.MinValue;
  44.         private int _maxValue = int.MaxValue;
  45.         private int _resolution = 1;
  46.        
  47.         public IntegerValidator(int minValue, int maxValue) : this(minValue, maxValue, false, 1)
  48.         {
  49.         }
  50.        
  51.         public IntegerValidator(int minValue, int maxValue, bool rangeIsExclusive) : this(minValue, maxValue, rangeIsExclusive, 1)
  52.         {
  53.         }
  54.        
  55.         public IntegerValidator(int minValue, int maxValue, bool rangeIsExclusive, int resolution)
  56.         {
  57.             if (resolution <= 0) {
  58.                 throw new ArgumentOutOfRangeException("resolution");
  59.             }
  60.            
  61.             if (minValue > maxValue) {
  62.                 throw new ArgumentOutOfRangeException("minValue", SR.GetString(SR.Validator_min_greater_than_max));
  63.             }
  64.            
  65.             _minValue = minValue;
  66.             _maxValue = maxValue;
  67.             _resolution = resolution;
  68.            
  69.             _flags = rangeIsExclusive ? ValidationFlags.ExclusiveRange : ValidationFlags.None;
  70.         }
  71.        
  72.         public override bool CanValidate(Type type)
  73.         {
  74.             return (type == typeof(int));
  75.         }
  76.        
  77.         public override void Validate(object value)
  78.         {
  79.             ValidatorUtils.HelperParamValidation(value, typeof(int));
  80.            
  81.             ValidatorUtils.ValidateScalar((int)value, _minValue, _maxValue, _resolution, _flags == ValidationFlags.ExclusiveRange);
  82.         }
  83.     }
  84. }

Developer Fusion