Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / Configuration / System / Configuration / GenericEnumConverter.cs / 1 / GenericEnumConverter.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; namespace System.Configuration { public sealed class GenericEnumConverter : ConfigurationConverterBase { private Type _enumType; public GenericEnumConverter(Type typeEnum) { if (typeEnum == null) { throw new ArgumentNullException("typeEnum"); } _enumType = typeEnum; } public override object ConvertTo(ITypeDescriptorContext ctx, CultureInfo ci, object value, Type type) { Debug.Assert(typeof(System.Enum).IsAssignableFrom(value.GetType()), "typeof(System.Enum).IsAssignableFrom(value.GetType())"); return value.ToString(); } public override object ConvertFrom(ITypeDescriptorContext ctx, CultureInfo ci, object data) { object result = null; // // For any error, throw the ArgumentException with SR.Invalid_enum_value // try { string value = (string)data; if (String.IsNullOrEmpty(value)) { throw new Exception(); } // Disallow numeric values for enums. if (!String.IsNullOrEmpty(value) && (Char.IsDigit(value[0]) || (value[0] == '-') || (value[0] == '+'))) { throw new Exception(); } if (value != value.Trim()) { // throw if the value has whitespace throw new Exception(); } result = Enum.Parse(_enumType, value); } catch { StringBuilder names = new StringBuilder(); foreach (string name in Enum.GetNames(_enumType)) { if (names.Length != 0) { names.Append(", "); } names.Append(name); } throw new ArgumentException(SR.GetString(SR.Invalid_enum_value, names.ToString())); } return result; } } } // 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; namespace System.Configuration { public sealed class GenericEnumConverter : ConfigurationConverterBase { private Type _enumType; public GenericEnumConverter(Type typeEnum) { if (typeEnum == null) { throw new ArgumentNullException("typeEnum"); } _enumType = typeEnum; } public override object ConvertTo(ITypeDescriptorContext ctx, CultureInfo ci, object value, Type type) { Debug.Assert(typeof(System.Enum).IsAssignableFrom(value.GetType()), "typeof(System.Enum).IsAssignableFrom(value.GetType())"); return value.ToString(); } public override object ConvertFrom(ITypeDescriptorContext ctx, CultureInfo ci, object data) { object result = null; // // For any error, throw the ArgumentException with SR.Invalid_enum_value // try { string value = (string)data; if (String.IsNullOrEmpty(value)) { throw new Exception(); } // Disallow numeric values for enums. if (!String.IsNullOrEmpty(value) && (Char.IsDigit(value[0]) || (value[0] == '-') || (value[0] == '+'))) { throw new Exception(); } if (value != value.Trim()) { // throw if the value has whitespace throw new Exception(); } result = Enum.Parse(_enumType, value); } catch { StringBuilder names = new StringBuilder(); foreach (string name in Enum.GetNames(_enumType)) { if (names.Length != 0) { names.Append(", "); } names.Append(name); } throw new ArgumentException(SR.GetString(SR.Invalid_enum_value, names.ToString())); } return result; } } } // 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
- TabItem.cs
- VersionPair.cs
- XmlAttribute.cs
- LabelTarget.cs
- Activator.cs
- PageCodeDomTreeGenerator.cs
- ClientData.cs
- ServiceNameElementCollection.cs
- SQLDouble.cs
- DocumentOrderQuery.cs
- WebEventCodes.cs
- ExpressionBuilder.cs
- BufferedGraphics.cs
- MobileCapabilities.cs
- CreateBookmarkScope.cs
- SiteMapDataSource.cs
- ErrorFormatterPage.cs
- PackageStore.cs
- ObjectHandle.cs
- CustomAttributeSerializer.cs
- FontInfo.cs
- TranslateTransform.cs
- XmlSchemaSimpleTypeUnion.cs
- XmlSchemaSearchPattern.cs
- safelink.cs
- DoubleLinkListEnumerator.cs
- ImageSource.cs
- ScriptControl.cs
- _RequestLifetimeSetter.cs
- FormViewRow.cs
- entitydatasourceentitysetnameconverter.cs
- SqlDataSourceQueryEditor.cs
- ReadOnlyDataSource.cs
- IISUnsafeMethods.cs
- MethodImplAttribute.cs
- AuthenticationModuleElement.cs
- DWriteFactory.cs
- InsufficientMemoryException.cs
- TextClipboardData.cs
- WindowsPrincipal.cs
- SourceFileBuildProvider.cs
- TimelineCollection.cs
- PaginationProgressEventArgs.cs
- XmlSchemaAppInfo.cs
- RbTree.cs
- NetworkCredential.cs
- AsyncPostBackErrorEventArgs.cs
- MarkupExtensionParser.cs
- UInt64Converter.cs
- ThreadAbortException.cs
- CommandEventArgs.cs
- VarRemapper.cs
- SqlConnectionFactory.cs
- DiagnosticTrace.cs
- RSAOAEPKeyExchangeDeformatter.cs
- ReachPageContentSerializer.cs
- Constants.cs
- Processor.cs
- COSERVERINFO.cs
- Utilities.cs
- followingquery.cs
- ForEachAction.cs
- CheckBox.cs
- AspNetSynchronizationContext.cs
- AbstractSvcMapFileLoader.cs
- TypeBinaryExpression.cs
- WindowsAuthenticationModule.cs
- PropertyMapper.cs
- UIElement3D.cs
- AxDesigner.cs
- StringAnimationBase.cs
- UpDownEvent.cs
- Avt.cs
- mediaeventshelper.cs
- DispatcherBuilder.cs
- CodeIterationStatement.cs
- Int64Storage.cs
- InheritanceAttribute.cs
- FunctionNode.cs
- AppDomain.cs
- SqlRemoveConstantOrderBy.cs
- ComboBox.cs
- BindingBase.cs
- Win32MouseDevice.cs
- CommentEmitter.cs
- DbConnectionStringCommon.cs
- PropertyChangedEventArgs.cs
- ClientConfigPaths.cs
- InstanceOwnerQueryResult.cs
- SoapAttributeAttribute.cs
- IntegerValidatorAttribute.cs
- AQNBuilder.cs
- RegistryPermission.cs
- InputScope.cs
- compensatingcollection.cs
- RemoteCryptoRsaServiceProvider.cs
- Int64AnimationBase.cs
- ResolveInfo.cs
- RbTree.cs
- WebEvents.cs