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
- FontNamesConverter.cs
- UpdateCommandGenerator.cs
- LoginName.cs
- RenderOptions.cs
- MissingMethodException.cs
- webproxy.cs
- GridItemPattern.cs
- InsufficientMemoryException.cs
- RequiredFieldValidator.cs
- ToolTip.cs
- RunInstallerAttribute.cs
- ValueSerializer.cs
- XmlValueConverter.cs
- PartEditor.cs
- TextTreeUndo.cs
- WebServiceData.cs
- DoubleCollectionConverter.cs
- DataColumnMappingCollection.cs
- XsdValidatingReader.cs
- NeedSkipTokenVisitor.cs
- _NegoStream.cs
- InputMethodStateTypeInfo.cs
- LoginName.cs
- LeafCellTreeNode.cs
- FormsAuthenticationUserCollection.cs
- UnauthorizedWebPart.cs
- InstanceLockLostException.cs
- ResourceAssociationType.cs
- MediaEntryAttribute.cs
- Expression.cs
- DataColumnSelectionConverter.cs
- InstanceStoreQueryResult.cs
- FormCollection.cs
- TypeSystem.cs
- RadialGradientBrush.cs
- MimeTypeMapper.cs
- UserControl.cs
- StorageMappingItemLoader.cs
- CreateParams.cs
- StackSpiller.cs
- TreeNodeEventArgs.cs
- StopStoryboard.cs
- WorkflowTerminatedException.cs
- ServiceInfo.cs
- UrlAuthorizationModule.cs
- ProcessModelInfo.cs
- DependencyPropertyValueSerializer.cs
- XmlWrappingReader.cs
- HelpEvent.cs
- TypeBuilder.cs
- AliasedSlot.cs
- PasswordTextContainer.cs
- ListItemCollection.cs
- AttachedPropertyBrowsableWhenAttributePresentAttribute.cs
- BitmapFrame.cs
- Menu.cs
- ProjectionCamera.cs
- AdditionalEntityFunctions.cs
- WindowsAuthenticationEventArgs.cs
- IIS7UserPrincipal.cs
- Crc32.cs
- SqlSelectStatement.cs
- TextTreeTextNode.cs
- SparseMemoryStream.cs
- BufferedGraphicsManager.cs
- GenericArgumentsUpdater.cs
- MappingMetadataHelper.cs
- UseLicense.cs
- Page.cs
- ButtonPopupAdapter.cs
- TableAdapterManagerGenerator.cs
- SvcMapFileSerializer.cs
- DataServiceHost.cs
- Table.cs
- XomlCompilerResults.cs
- PresentationTraceSources.cs
- BinaryFormatter.cs
- UInt64.cs
- OneWayChannelListener.cs
- QuadraticBezierSegment.cs
- DependencyObjectProvider.cs
- DesignTimeParseData.cs
- EntityContainer.cs
- QueryableFilterUserControl.cs
- CustomPopupPlacement.cs
- InstanceDataCollection.cs
- RegularExpressionValidator.cs
- ClientApiGenerator.cs
- XamlReader.cs
- StaticSiteMapProvider.cs
- RoleService.cs
- ValidatorCollection.cs
- GridItem.cs
- TypeBuilder.cs
- SHA1Managed.cs
- UriParserTemplates.cs
- EntityCommandCompilationException.cs
- NotImplementedException.cs
- CompilerError.cs
- StreamInfo.cs