Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / Configuration / System / Configuration / ValidatorUtils.cs / 1 / ValidatorUtils.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Security.Permissions;
using System.Xml;
using System.Collections.Specialized;
using System.Globalization;
using System.ComponentModel;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
namespace System.Configuration {
internal static class ValidatorUtils {
public static void HelperParamValidation(object value, Type allowedType) {
if (value == null) {
return;
}
if (value.GetType() != allowedType) {
throw new ArgumentException(SR.GetString(SR.Validator_value_type_invalid), String.Empty);
}
}
public static void ValidateScalar(T value, T min, T max, T resolution, bool exclusiveRange) where T : IComparable {
ValidateRangeImpl(value, min, max, exclusiveRange);
// Validate the resolution
ValidateResolution(resolution.ToString(), Convert.ToInt64(value, CultureInfo.InvariantCulture), Convert.ToInt64(resolution, CultureInfo.InvariantCulture));
}
private static void ValidateRangeImpl( T value, T min, T max, bool exclusiveRange ) where T : IComparable {
IComparable itfValue = (IComparable)value;
IComparable itfMax = (IComparable)max;
bool valueIsInRange = false;
// Check min range
if ( itfValue.CompareTo( min ) >= 0 ) {
// TRUE: value >= min
valueIsInRange = true;
}
if ( valueIsInRange && ( itfValue.CompareTo( max ) > 0 ) ) {
// TRUE: value > max
valueIsInRange = false;
}
// Throw range validation error
if ( !( valueIsInRange ^ exclusiveRange ) ) {
string error = null;
// First group of errors - the min and max range are the same. i.e. the valid value must be the same/equal to the min(max)
if ( min.Equals( max ) ) {
if ( exclusiveRange ) {
// Valid values are outside of range. I.e has to be different then min( or max )
error = SR.GetString( SR.Validation_scalar_range_violation_not_different );
}
else {
// Valid values are inside the range. I.e. has to be equal to min ( or max )
error = SR.GetString( SR.Validation_scalar_range_violation_not_equal );
}
}
// Second group of errors: min != max. I.e. its a range
else {
if ( exclusiveRange ) {
// Valid values are outside of range.
error = SR.GetString( SR.Validation_scalar_range_violation_not_outside_range );
}
else {
// Valid values are inside the range. I.e. has to be equal to min ( or max )
error = SR.GetString( SR.Validation_scalar_range_violation_not_in_range );
}
}
throw new ArgumentException( String.Format( CultureInfo.InvariantCulture,
error,
min.ToString(),
max.ToString() ) );
}
}
private static void ValidateResolution(string resolutionAsString, long value, long resolution) {
Debug.Assert(resolution > 0, "resolution > 0");
if ((value % resolution) != 0) {
throw new ArgumentException(SR.GetString(SR.Validator_scalar_resolution_violation, resolutionAsString));
}
}
public static void ValidateScalar(TimeSpan value, TimeSpan min, TimeSpan max, long resolutionInSeconds, bool exclusiveRange) {
ValidateRangeImpl(value, min, max, exclusiveRange);
// Validate the resolution
if (resolutionInSeconds > 0) {
ValidateResolution(TimeSpan.FromSeconds( resolutionInSeconds ).ToString(), value.Ticks, resolutionInSeconds * TimeSpan.TicksPerSecond);
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Security.Permissions;
using System.Xml;
using System.Collections.Specialized;
using System.Globalization;
using System.ComponentModel;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
namespace System.Configuration {
internal static class ValidatorUtils {
public static void HelperParamValidation(object value, Type allowedType) {
if (value == null) {
return;
}
if (value.GetType() != allowedType) {
throw new ArgumentException(SR.GetString(SR.Validator_value_type_invalid), String.Empty);
}
}
public static void ValidateScalar(T value, T min, T max, T resolution, bool exclusiveRange) where T : IComparable {
ValidateRangeImpl(value, min, max, exclusiveRange);
// Validate the resolution
ValidateResolution(resolution.ToString(), Convert.ToInt64(value, CultureInfo.InvariantCulture), Convert.ToInt64(resolution, CultureInfo.InvariantCulture));
}
private static void ValidateRangeImpl( T value, T min, T max, bool exclusiveRange ) where T : IComparable {
IComparable itfValue = (IComparable)value;
IComparable itfMax = (IComparable)max;
bool valueIsInRange = false;
// Check min range
if ( itfValue.CompareTo( min ) >= 0 ) {
// TRUE: value >= min
valueIsInRange = true;
}
if ( valueIsInRange && ( itfValue.CompareTo( max ) > 0 ) ) {
// TRUE: value > max
valueIsInRange = false;
}
// Throw range validation error
if ( !( valueIsInRange ^ exclusiveRange ) ) {
string error = null;
// First group of errors - the min and max range are the same. i.e. the valid value must be the same/equal to the min(max)
if ( min.Equals( max ) ) {
if ( exclusiveRange ) {
// Valid values are outside of range. I.e has to be different then min( or max )
error = SR.GetString( SR.Validation_scalar_range_violation_not_different );
}
else {
// Valid values are inside the range. I.e. has to be equal to min ( or max )
error = SR.GetString( SR.Validation_scalar_range_violation_not_equal );
}
}
// Second group of errors: min != max. I.e. its a range
else {
if ( exclusiveRange ) {
// Valid values are outside of range.
error = SR.GetString( SR.Validation_scalar_range_violation_not_outside_range );
}
else {
// Valid values are inside the range. I.e. has to be equal to min ( or max )
error = SR.GetString( SR.Validation_scalar_range_violation_not_in_range );
}
}
throw new ArgumentException( String.Format( CultureInfo.InvariantCulture,
error,
min.ToString(),
max.ToString() ) );
}
}
private static void ValidateResolution(string resolutionAsString, long value, long resolution) {
Debug.Assert(resolution > 0, "resolution > 0");
if ((value % resolution) != 0) {
throw new ArgumentException(SR.GetString(SR.Validator_scalar_resolution_violation, resolutionAsString));
}
}
public static void ValidateScalar(TimeSpan value, TimeSpan min, TimeSpan max, long resolutionInSeconds, bool exclusiveRange) {
ValidateRangeImpl(value, min, max, exclusiveRange);
// Validate the resolution
if (resolutionInSeconds > 0) {
ValidateResolution(TimeSpan.FromSeconds( resolutionInSeconds ).ToString(), value.Ticks, resolutionInSeconds * TimeSpan.TicksPerSecond);
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TypeConverterHelper.cs
- StateDesigner.Layouts.cs
- WebPartConnectionsCloseVerb.cs
- ValidatedControlConverter.cs
- CodeAccessPermission.cs
- BindingGraph.cs
- CodeGroup.cs
- ArrayElementGridEntry.cs
- rsa.cs
- RelationshipFixer.cs
- StylusTip.cs
- SingleAnimationBase.cs
- QilName.cs
- ResourceSet.cs
- MTConfigUtil.cs
- WindowProviderWrapper.cs
- ModuleElement.cs
- MessageDroppedTraceRecord.cs
- SimpleTableProvider.cs
- _UriTypeConverter.cs
- CustomError.cs
- DateTimeConstantAttribute.cs
- DataGridPageChangedEventArgs.cs
- EndpointFilterProvider.cs
- XpsSerializerFactory.cs
- TextFindEngine.cs
- WebExceptionStatus.cs
- ClientProtocol.cs
- ConfigsHelper.cs
- EntityContainerEmitter.cs
- GlyphShapingProperties.cs
- _ListenerRequestStream.cs
- SendMailErrorEventArgs.cs
- DataRecord.cs
- SQLInt32Storage.cs
- ViewStateModeByIdAttribute.cs
- SafeBitVector32.cs
- LogicalCallContext.cs
- SyndicationFeed.cs
- ThreadExceptionDialog.cs
- Message.cs
- DrawingImage.cs
- SafeLibraryHandle.cs
- DoubleAnimationUsingPath.cs
- StringTraceRecord.cs
- WindowsComboBox.cs
- TrailingSpaceComparer.cs
- WeakKeyDictionary.cs
- DayRenderEvent.cs
- SoapFormatterSinks.cs
- LinkArea.cs
- DBConcurrencyException.cs
- PrincipalPermission.cs
- Executor.cs
- IdnElement.cs
- ReachSerializableProperties.cs
- OdbcHandle.cs
- UnsafeNativeMethods.cs
- WebPartEventArgs.cs
- ClientProxyGenerator.cs
- InternalsVisibleToAttribute.cs
- StructuralObject.cs
- LookupNode.cs
- AbsoluteQuery.cs
- HttpCapabilitiesSectionHandler.cs
- SizeLimitedCache.cs
- AudioDeviceOut.cs
- StorageFunctionMapping.cs
- XmlCodeExporter.cs
- Helper.cs
- SQLInt64.cs
- NetCodeGroup.cs
- X509ServiceCertificateAuthenticationElement.cs
- AppDomainFactory.cs
- IdentityModelDictionary.cs
- TextClipboardData.cs
- XmlSchemaExternal.cs
- precedingquery.cs
- ToolBar.cs
- CommonObjectSecurity.cs
- HtmlShim.cs
- BuilderElements.cs
- DataGridViewComboBoxColumn.cs
- _Win32.cs
- DetailsViewInsertedEventArgs.cs
- Normalization.cs
- HostExecutionContextManager.cs
- DbProviderFactories.cs
- SendMailErrorEventArgs.cs
- RenderTargetBitmap.cs
- JsonObjectDataContract.cs
- DragDrop.cs
- DelegateSerializationHolder.cs
- ErrorFormatterPage.cs
- StateBag.cs
- DecoderBestFitFallback.cs
- GcHandle.cs
- ListViewTableRow.cs
- InfoCardHelper.cs
- VisualBrush.cs