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
- FontNamesConverter.cs
- LiteralLink.cs
- ProxyGenerationError.cs
- MetadataItem.cs
- FrameworkRichTextComposition.cs
- XPathScanner.cs
- BindingContext.cs
- EntityViewGenerationConstants.cs
- AxHost.cs
- SymmetricKeyWrap.cs
- ToolboxItemAttribute.cs
- metadatamappinghashervisitor.cs
- MdImport.cs
- BrushMappingModeValidation.cs
- odbcmetadatafactory.cs
- ProgressBar.cs
- XamlInt32CollectionSerializer.cs
- XmlLoader.cs
- PasswordRecovery.cs
- WindowsProgressbar.cs
- AttributeAction.cs
- TreeNodeConverter.cs
- FixedSOMContainer.cs
- CfgRule.cs
- ThemeDirectoryCompiler.cs
- ConvertEvent.cs
- RegistryDataKey.cs
- SortFieldComparer.cs
- EnumerableRowCollection.cs
- SqlStatistics.cs
- SecurityStandardsManager.cs
- XmlNode.cs
- EventMetadata.cs
- DataGridViewColumnCollection.cs
- StateMachine.cs
- TerminateSequenceResponse.cs
- RichTextBoxAutomationPeer.cs
- VectorValueSerializer.cs
- EventPropertyMap.cs
- GeneralTransformGroup.cs
- DefaultParameterValueAttribute.cs
- SqlDesignerDataSourceView.cs
- ConfigurationManagerHelper.cs
- Main.cs
- DesignerVerbCollection.cs
- TreeNodeMouseHoverEvent.cs
- NoneExcludedImageIndexConverter.cs
- GridViewPageEventArgs.cs
- handlecollector.cs
- PathSegmentCollection.cs
- RelationshipEntry.cs
- EncodingDataItem.cs
- FileVersion.cs
- CatalogPart.cs
- GlyphRunDrawing.cs
- ChannelCredentials.cs
- ComplexLine.cs
- ExtenderControl.cs
- DesignTimeTemplateParser.cs
- PackageDigitalSignature.cs
- XmlImplementation.cs
- QuaternionValueSerializer.cs
- Msec.cs
- TransformedBitmap.cs
- Pts.cs
- EntityReference.cs
- ConfigXmlSignificantWhitespace.cs
- SqlCacheDependencyDatabaseCollection.cs
- Identity.cs
- SessionEndedEventArgs.cs
- Asn1IntegerConverter.cs
- ContentPathSegment.cs
- CustomErrorCollection.cs
- AppSecurityManager.cs
- HtmlInputReset.cs
- StylusPointProperties.cs
- WpfKnownMember.cs
- IdleTimeoutMonitor.cs
- SetStoryboardSpeedRatio.cs
- RPIdentityRequirement.cs
- Utils.cs
- XmlPreloadedResolver.cs
- PreservationFileReader.cs
- DbConnectionPoolGroupProviderInfo.cs
- PrintDialog.cs
- StrokeDescriptor.cs
- ConfigXmlComment.cs
- SystemDiagnosticsSection.cs
- MergeLocalizationDirectives.cs
- COM2AboutBoxPropertyDescriptor.cs
- EnglishPluralizationService.cs
- ProcessModuleDesigner.cs
- HttpServerVarsCollection.cs
- SvcMapFileLoader.cs
- NativeDirectoryServicesQueryAPIs.cs
- WizardForm.cs
- DesignerWebPartChrome.cs
- OrderedDictionary.cs
- PeerNameRegistration.cs
- InstanceDataCollectionCollection.cs