Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Configuration / System / Configuration / GenericEnumConverter.cs / 1305376 / 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
- EnumType.cs
- BuildManager.cs
- StickyNoteContentControl.cs
- SqlTypesSchemaImporter.cs
- SchemaObjectWriter.cs
- ChangeNode.cs
- Screen.cs
- LicenseException.cs
- ConfigurationElementProperty.cs
- DropDownButton.cs
- Stopwatch.cs
- ValueSerializerAttribute.cs
- CompensationToken.cs
- HttpCapabilitiesSectionHandler.cs
- Path.cs
- ObjectDataSourceMethodEventArgs.cs
- EventLogEntry.cs
- MetadataArtifactLoaderComposite.cs
- XmlQueryType.cs
- PagerStyle.cs
- FreezableCollection.cs
- ACE.cs
- UInt64Converter.cs
- ButtonRenderer.cs
- PaginationProgressEventArgs.cs
- NumericUpDownAccelerationCollection.cs
- BmpBitmapDecoder.cs
- HtmlHead.cs
- WindowsGraphicsCacheManager.cs
- AxImporter.cs
- ArrowControl.xaml.cs
- UidPropertyAttribute.cs
- DispatcherExceptionEventArgs.cs
- SqlPersistenceProviderFactory.cs
- ReaderContextStackData.cs
- EpmCustomContentWriterNodeData.cs
- VirtualPathProvider.cs
- UTF8Encoding.cs
- KoreanLunisolarCalendar.cs
- ObjRef.cs
- FileStream.cs
- MediaScriptCommandRoutedEventArgs.cs
- SharedStatics.cs
- RuleSetDialog.Designer.cs
- SqlDataSourceView.cs
- EventTrigger.cs
- CodeTypeParameterCollection.cs
- AuthenticationConfig.cs
- WpfXamlType.cs
- StatusBarAutomationPeer.cs
- ErrorStyle.cs
- TableLayoutSettingsTypeConverter.cs
- WindowShowOrOpenTracker.cs
- RuleSettingsCollection.cs
- CleanUpVirtualizedItemEventArgs.cs
- DataControlPagerLinkButton.cs
- TabControl.cs
- AttributeAction.cs
- ControlEvent.cs
- SynchronizingStream.cs
- BindingMAnagerBase.cs
- RtfToXamlReader.cs
- CodeDirectoryCompiler.cs
- TypefaceMetricsCache.cs
- AssertHelper.cs
- ValueSerializer.cs
- SoapHeaderAttribute.cs
- InfoCardArgumentException.cs
- ProfilePropertySettings.cs
- LOSFormatter.cs
- GridViewCellAutomationPeer.cs
- OdbcConnectionStringbuilder.cs
- SqlDeflator.cs
- XmlBinaryWriter.cs
- BlurEffect.cs
- StylusPointProperties.cs
- TextRunTypographyProperties.cs
- ListParaClient.cs
- PropertyDescriptorCollection.cs
- DocComment.cs
- SyncMethodInvoker.cs
- AnonymousIdentificationModule.cs
- XmlException.cs
- FamilyTypefaceCollection.cs
- SmtpCommands.cs
- PrimitiveCodeDomSerializer.cs
- SchemaManager.cs
- fixedPageContentExtractor.cs
- _KerberosClient.cs
- Geometry3D.cs
- infer.cs
- OneToOneMappingSerializer.cs
- MetadataCache.cs
- SimpleFileLog.cs
- RegisteredExpandoAttribute.cs
- FillErrorEventArgs.cs
- exports.cs
- List.cs
- ArrayHelper.cs
- PathFigureCollectionValueSerializer.cs