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
- InplaceBitmapMetadataWriter.cs
- FieldToken.cs
- EventSourceCreationData.cs
- DataListCommandEventArgs.cs
- ComplexLine.cs
- RecognizedAudio.cs
- DotExpr.cs
- XmlSchemaException.cs
- EventSinkHelperWriter.cs
- EntityTypeEmitter.cs
- LinkArea.cs
- UInt64.cs
- AssemblyBuilder.cs
- DataBinding.cs
- DoubleLinkList.cs
- CqlBlock.cs
- RepeaterItemEventArgs.cs
- CollectionViewGroupRoot.cs
- DisplayInformation.cs
- DataGridAutoFormatDialog.cs
- EmptyElement.cs
- DataColumnCollection.cs
- ErrorStyle.cs
- AggregateNode.cs
- AdPostCacheSubstitution.cs
- DecimalStorage.cs
- XmlILModule.cs
- ResolveDuplex11AsyncResult.cs
- RawMouseInputReport.cs
- FileCodeGroup.cs
- XsltException.cs
- DesignSurfaceCollection.cs
- Queue.cs
- HttpModuleActionCollection.cs
- BridgeDataRecord.cs
- SerTrace.cs
- StringHelper.cs
- WebBrowser.cs
- CodeObjectCreateExpression.cs
- IteratorFilter.cs
- SmtpNegotiateAuthenticationModule.cs
- _SecureChannel.cs
- MyContact.cs
- HashCodeCombiner.cs
- HttpBufferlessInputStream.cs
- EllipticalNodeOperations.cs
- PropertyDescriptorComparer.cs
- PeerNameResolver.cs
- ObjectView.cs
- PageContentCollection.cs
- BaseEntityWrapper.cs
- XmlArrayItemAttributes.cs
- ValueTypeFixupInfo.cs
- _emptywebproxy.cs
- ImageResources.Designer.cs
- Propagator.JoinPropagator.JoinPredicateVisitor.cs
- CurrentChangingEventManager.cs
- XPathDocumentIterator.cs
- NonClientArea.cs
- MailAddress.cs
- DependencyObjectProvider.cs
- recordstatefactory.cs
- StandardOleMarshalObject.cs
- DbModificationCommandTree.cs
- RepeatButton.cs
- RequestCacheManager.cs
- HttpContext.cs
- ProcessHostServerConfig.cs
- XamlPoint3DCollectionSerializer.cs
- odbcmetadatafactory.cs
- BrowserTree.cs
- XmlDocumentType.cs
- CheckBox.cs
- NativeMethods.cs
- LayoutTableCell.cs
- WebPartRestoreVerb.cs
- ColorComboBox.cs
- KnownTypeAttribute.cs
- XmlUtil.cs
- Util.cs
- MissingSatelliteAssemblyException.cs
- ElementProxy.cs
- HttpFormatExtensions.cs
- SerializationSectionGroup.cs
- GridViewItemAutomationPeer.cs
- recordstate.cs
- PenContext.cs
- BufferedReadStream.cs
- ReliabilityContractAttribute.cs
- ImportFileRequest.cs
- ResXFileRef.cs
- SrgsSubset.cs
- RegularExpressionValidator.cs
- ChameleonKey.cs
- DbConnectionPoolGroupProviderInfo.cs
- PtsContext.cs
- KnownTypesProvider.cs
- LayoutInformation.cs
- PageTheme.cs
- PassportIdentity.cs