Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / 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
- ProgressPage.cs
- IsolatedStorageException.cs
- AbandonedMutexException.cs
- StructuredCompositeActivityDesigner.cs
- WorkflowDebuggerSteppingAttribute.cs
- DoubleAnimationBase.cs
- GeneralTransform3D.cs
- diagnosticsswitches.cs
- CompleteWizardStep.cs
- RedistVersionInfo.cs
- MethodBuilderInstantiation.cs
- ArgIterator.cs
- ObjectSecurityT.cs
- KnownIds.cs
- KnownColorTable.cs
- CallbackException.cs
- ContentPlaceHolderDesigner.cs
- COM2ICategorizePropertiesHandler.cs
- FunctionParameter.cs
- BasePropertyDescriptor.cs
- SpecialNameAttribute.cs
- AdjustableArrowCap.cs
- PathFigure.cs
- srgsitem.cs
- SynchronizationContext.cs
- XsltArgumentList.cs
- ConfigurationStrings.cs
- MeasureData.cs
- RemotingConfigParser.cs
- WebPartDisplayModeCancelEventArgs.cs
- FlagPanel.cs
- SqlExpander.cs
- XsdCachingReader.cs
- ToolboxCategory.cs
- OneToOneMappingSerializer.cs
- XAMLParseException.cs
- WebBrowser.cs
- AspNetSynchronizationContext.cs
- FileSystemWatcher.cs
- DbCommandDefinition.cs
- MenuCommand.cs
- DataGridCell.cs
- HtmlTernaryTree.cs
- SharedPerformanceCounter.cs
- IISMapPath.cs
- FixedSOMPageElement.cs
- ELinqQueryState.cs
- TypeDependencyAttribute.cs
- WebPartEditorApplyVerb.cs
- EventProvider.cs
- FrameworkTextComposition.cs
- UserPreferenceChangedEventArgs.cs
- MissingMemberException.cs
- MdiWindowListStrip.cs
- MediaTimeline.cs
- CachedPathData.cs
- SqlClientFactory.cs
- CharEntityEncoderFallback.cs
- BmpBitmapDecoder.cs
- SpellerError.cs
- InteropBitmapSource.cs
- WhitespaceSignificantCollectionAttribute.cs
- FixedTextBuilder.cs
- TableRow.cs
- SplineKeyFrames.cs
- SqlFlattener.cs
- DataSourceView.cs
- Calendar.cs
- Emitter.cs
- DigitalSignatureProvider.cs
- InputProviderSite.cs
- CharEntityEncoderFallback.cs
- HealthMonitoringSection.cs
- CompiledAction.cs
- PropertyEmitter.cs
- CreateRefExpr.cs
- Stack.cs
- EdmSchemaError.cs
- AnnotationComponentManager.cs
- DependencyPropertyValueSerializer.cs
- FontStyleConverter.cs
- DataGridColumn.cs
- TcpPortSharing.cs
- BamlBinaryWriter.cs
- SqlDataSourceConfigureFilterForm.cs
- ApplicationServiceHelper.cs
- UncommonField.cs
- InfoCardAsymmetricCrypto.cs
- SignedXml.cs
- ScrollContentPresenter.cs
- ListControlBuilder.cs
- EventWaitHandleSecurity.cs
- CultureInfoConverter.cs
- WebBrowser.cs
- XmlQueryContext.cs
- BinaryFormatterSinks.cs
- BinaryWriter.cs
- WorkflowRuntimeService.cs
- DbSource.cs
- FileReservationCollection.cs