PositiveTimeSpanValidator Class
Provides validation of a TimeSpan object. This class cannot be inherited.
System.Configuration.ConfigurationValidatorBase
System.Configuration.PositiveTimeSpanValidator
Assembly: System.Configuration (in System.Configuration.dll)
The PositiveTimeSpanValidator type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | PositiveTimeSpanValidator | Initializes a new instance of the PositiveTimeSpanValidator class. |
| Name | Description | |
|---|---|---|
![]() | CanValidate | Determines whether the object type can be validated. (Overrides ConfigurationValidatorBase.CanValidate(Type).) |
![]() | Equals(Object) | Determines whether the specified object is equal to the current object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
![]() | Validate | Determines whether the value of an object is valid. (Overrides ConfigurationValidatorBase.Validate(Object).) |
The PositiveTimeSpanValidator is used to ensure a TimeSpan object meets specific criteria. The CanValidate method determines whether the object type being validated matches the expected type. The object being validated is passed as a parameter of the Validate method. To pass validation, the object being validated must be a positive TimeSpan value.
The following code example demonstrates how to use the PositiveTimeSpanValidator type.
using System; using System.Collections.Generic; using System.Text; using System.Configuration; using System.ComponentModel; namespace Samples.AspNet { // Implements a custom validator attribute. [AttributeUsage(AttributeTargets.Property)] public sealed class CustomValidatorAttribute : ConfigurationValidatorAttribute { public CustomValidatorAttribute() { } public CustomValidatorAttribute(Type validator) : base(validator) { } new public Type ValidatorType { get { return GetType(); } } public override ConfigurationValidatorBase ValidatorInstance { get { // Create validator. return new PositiveTimeSpanValidator(); } } } // Implements a custom section class. public class SampleSection : ConfigurationSection { [ConfigurationProperty("name", DefaultValue = "MyBuildRoutine", IsRequired = true)] [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } [ConfigurationProperty("BuildStartTime", IsRequired = true, DefaultValue = "09:00:00")] public TimeSpan BuildStartTime { get { TimeSpanConverter myTSC = new TimeSpanConverter(); return (TimeSpan)this["BuildStartTime"]; } set { this["BuildStartTime"] = value.ToString(); } } [ConfigurationProperty("BuildEndTime", IsRequired = true, DefaultValue = "17:00:00")] public TimeSpan BuildEndTime { get { TimeSpanConverter myTSC = new TimeSpanConverter(); return (TimeSpan)this["BuildEndTime"]; } set { this["BuildEndTime"] = value.ToString(); } } } // Implements the console user interface. class TestingCustomValidatorAttribute { // Shows how to use the ValidatorInstance method. public static void GetCustomValidatorInstance() { ConfigurationValidatorBase valBase; CustomValidatorAttribute customValAttr; customValAttr = new CustomValidatorAttribute(); SampleSection sampleSection = ConfigurationManager.GetSection("MyDailyProcess") as SampleSection; TimeSpanConverter myTSC = new TimeSpanConverter(); TimeSpan StartTimeSpan = (TimeSpan)myTSC.ConvertFromString(sampleSection.BuildStartTime.ToString()); TimeSpan EndTimeSpan = (TimeSpan)myTSC.ConvertFromString(sampleSection.BuildEndTime.ToString()); TimeSpan resultTimeSpan = EndTimeSpan - StartTimeSpan; try { // Determine if the Validator can validate // the type it contains. valBase = customValAttr.ValidatorInstance; if (valBase.CanValidate(resultTimeSpan.GetType())) { // Validate the TimeSpan using a // custom PositiveTimeSpanValidator. valBase.Validate(resultTimeSpan); } } catch (ArgumentException e) { // Store error message. string msg = e.Message.ToString(); #if DEBUG Console.WriteLine("Error: {0}", msg); #endif } } // Create required sections. static void CreateSection() { // Get the current configuration (file). System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Define the sample section. SampleSection sampleSection; // Create the handler section named MyDailyProcess // in the <configSections>. Also, create the // <MyDailyProcess> target section // in <configuration>. if (config.Sections["MyDailyProcess"] == null) { sampleSection = new SampleSection(); config.Sections.Add("MyDailyProcess", sampleSection); sampleSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } } static void DisplaySectionProperties() { SampleSection sampleSection = ConfigurationManager.GetSection("MyDailyProcess") as SampleSection; if (sampleSection == null) Console.WriteLine("Failed to load section."); else { Console.WriteLine("Defaults:"); Console.WriteLine(" Name: {0}", sampleSection.Name); Console.WriteLine(" BuildStartTime: {0}", sampleSection.BuildStartTime); Console.WriteLine(" BuildEndTime: {0}", sampleSection.BuildEndTime); } } static void Main(string[] args) { Console.WriteLine("[Create a section]"); CreateSection(); Console.WriteLine("[Display section information]"); DisplaySectionProperties(); // Show how to use the ValidatorInstance method. GetCustomValidatorInstance(); // Display and wait. Console.ReadLine(); } } }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
