Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System / net / System / Net / Configuration / TimeoutValidationAttribute.cs
1 namespace System.Net {
2     using System;
3     using System.Configuration;
4     using System.ComponentModel;
5     
6     // NOTE [Microsoft]: The old validation attribute was removed from System.ll and is     
7     // replaced by more flexible and robust validation/conversion design.
8     // The change bellow is a simple fix to make things work with the least possible change ( it is an integration break )
9     // However, we already have a built-in support for configuration properties that store     
10     // Type names. We do reccomend that all uses of the validator bellow are converted to
11     // properties of type Type ( instead of string ) which use the TypeNameConverter from System.Configuration.dll
12     // Feel free to ask me for more details if you decide to do the conversion
13     internal sealed class TimeoutValidator : ConfigurationValidatorBase
14     {
15         bool _zeroValid = false;
16
17         internal TimeoutValidator(bool zeroValid) {
18             _zeroValid = zeroValid;
19         }
20
21         public override bool CanValidate( Type type ) {
22             return ( type == typeof( int ) || type == typeof( long ) );
23         }
24
25         public override void Validate( object value ) {
26             if (value == null)
27                 return;
28             
29             int timeout = (int)value;
30             
31             if (_zeroValid && timeout == 0)
32                 return;
33             
34             if (timeout <= 0 && timeout != System.Threading.Timeout.Infinite) {
35                 // Note [Microsoft] : This is a lab integration fix. Old code did not have any error message at this point
36                 // This code change accomplishes the same result. However its highly reccomended that a specific error message is givven
37                 // to the user so they know what exaclty is the problem ( i.e. the value must be a positive integer or be Infinite )
38                 // To accomplish this - an exception with the specific error message could be thrown ( ArgumentException is prefferred )            
39                 throw new ConfigurationErrorsException(SR.GetString(SR.net_io_timeout_use_gt_zero));
40             }
41         }
42     }
43 }