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

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

Developer Fusion