System.Configuration.IntegerValidator 类

方法描述

对 Int32 值进行验证。

语法定义(C# System.Configuration.IntegerValidator 类 的用法)

public class IntegerValidator : ConfigurationValidatorBase

构造函数

构造函数名称 构造函数描述
IntegerValidator(Int32, Int32) 初始化 IntegerValidator 类的新实例。
IntegerValidator(Int32, Int32, Boolean) 初始化 IntegerValidator 类的新实例。
IntegerValidator(Int32, Int32, Boolean, Int32) 初始化 IntegerValidator 类的新实例。

成员/方法

方法名称 方法描述
CanValidate 确定是否可以验证该对象的类型。 (重写 ConfigurationValidatorBase.CanValidate(Type)。)
Equals(Object) 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
GetType 获取当前实例的 Type。 (继承自 Object。)
MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
ToString 返回表示当前对象的字符串。 (继承自 Object。)
Validate 确定对象的值是否有效。 (重写 ConfigurationValidatorBase.Validate(Object)。)

提示和注释

IntegerValidator 类用于确保某个整数满足特定条件。 验证条件是在创建 IntegerValidator 类的实例时建立的。 带两个参数的 IntegerValidator 构造函数确保要验证的整数符合最小和最大值限制。 带三个参数的 IntegerValidator 构造函数会检查最小和最大的 Int32 值,并检查该值是否位于指定的范围内。 带四个参数的 IntegerValidator 构造函数除检查前面三个参数外,还检查 Int32 值是否与一个特定解析相等。

CanValidate 方法可以确定正在接受验证的对象类型是否与所需要的类型相匹配。 被验证的对象作为 Validate 方法的参数传递。

System.Configuration.IntegerValidator 类例子

下面的代码示例演示如何使用 IntegerValidator 类型。

using System;
using System.Configuration;

namespace Microsoft.Samples.AspNet.Validators
{
    class UsingIntegerValidator
    {
        static void Main(string[] args)
        {
            // Display title.
            Console.WriteLine("ASP.NET Validators");
            Console.WriteLine();

            Console.WriteLine(
                "Set mininum and maximum of 1 and 10 inclusive");

            // Create Validator for the range of 1 to 10 inclusive
            int minIntVal = 1;
            int maxIntVal = 10;
            bool exclusive = false;
            IntegerValidator integerValidator =
                new IntegerValidator(minIntVal, maxIntVal, exclusive);

            int testInt = 0;
            ValidateInteger(integerValidator, testInt);
            testInt = 1;
            ValidateInteger(integerValidator, testInt);
            testInt = 5;
            ValidateInteger(integerValidator, testInt);

            Console.WriteLine();
            Console.WriteLine(
                "Set mininum and maximum of 1 and 10 exclusive");

            // Create Validator for the range of 1 to 10 exclusive
            exclusive = true;
            integerValidator =
                new IntegerValidator(minIntVal, maxIntVal, exclusive);

            testInt = 0;
            ValidateInteger(integerValidator, testInt);
            testInt = 1;
            ValidateInteger(integerValidator, testInt);
            testInt = 5;
            ValidateInteger(integerValidator, testInt);

            Console.WriteLine();
            Console.WriteLine(
                "Determine if an object to validate can be validated.");

            object testObjectToValidate = "a";
            Console.WriteLine("Can validate object of type {0}: {1}",
                testObjectToValidate.GetType(),
                integerValidator.CanValidate(testObjectToValidate.GetType()));
            testObjectToValidate = new int();
            Console.WriteLine("Can validate object of type {0}: {1}",
                testObjectToValidate.GetType(),
                integerValidator.CanValidate(testObjectToValidate.GetType()));

            // Leave output on screen until enter is pressed.
            Console.ReadLine();
        }

        private static void ValidateInteger(IntegerValidator integerValidator, int valuetoValidate)
        {
            Console.Write("Validating integer value of {0}:  ", valuetoValidate);
            try
            {
                integerValidator.Validate(valuetoValidate);
                Console.WriteLine("Validated.");
            }
            catch (ArgumentException e)
            {
                Console.WriteLine("Failed validation.  Message: {0}", e.Message.ToString());
            }
        }
    }
}

继承层次结构

System.Object

System.Configuration.ConfigurationValidatorBase

System.Configuration.IntegerValidator

命名空间

namespace: System.Configuration

程序集: System.Configuration(在 System.Configuration.dll 中)

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。