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
- PenLineCapValidation.cs
- StringPropertyBuilder.cs
- X509PeerCertificateAuthentication.cs
- SoapProcessingBehavior.cs
- TempEnvironment.cs
- ConnectionManagementElement.cs
- SafeHandle.cs
- ConfigXmlWhitespace.cs
- ImageConverter.cs
- AttributeQuery.cs
- XmlNavigatorStack.cs
- BulletDecorator.cs
- OpCodes.cs
- TriState.cs
- XmlNamespaceManager.cs
- QueryContinueDragEventArgs.cs
- RegisteredScript.cs
- ParameterModifier.cs
- ZipIOFileItemStream.cs
- ConditionedDesigner.cs
- DiscreteKeyFrames.cs
- ToolStripItemClickedEventArgs.cs
- TableRow.cs
- SelectedPathEditor.cs
- ProtocolsConfigurationEntry.cs
- AnnotationHelper.cs
- SamlAssertionKeyIdentifierClause.cs
- LicenseContext.cs
- ConsumerConnectionPointCollection.cs
- EventEntry.cs
- NetCodeGroup.cs
- FillErrorEventArgs.cs
- DashStyle.cs
- MergeFilterQuery.cs
- WasEndpointConfigContainer.cs
- DataSourceView.cs
- OdbcReferenceCollection.cs
- webbrowsersite.cs
- SizeValueSerializer.cs
- GeometryCollection.cs
- WebServiceErrorEvent.cs
- ContentFileHelper.cs
- WindowsFormsHostPropertyMap.cs
- Int64.cs
- TypeSemantics.cs
- EdmEntityTypeAttribute.cs
- ExtensionQuery.cs
- TransactionManagerProxy.cs
- ScriptingJsonSerializationSection.cs
- EmptyEnumerator.cs
- Rect3DValueSerializer.cs
- StylusPlugInCollection.cs
- NativeMethodsOther.cs
- TableRowCollection.cs
- IdentityValidationException.cs
- HandlerWithFactory.cs
- PropertyMappingExceptionEventArgs.cs
- MatcherBuilder.cs
- Pen.cs
- NameValueSectionHandler.cs
- RelatedView.cs
- GroupBox.cs
- LineBreakRecord.cs
- DataGridViewColumnHeaderCell.cs
- SqlBulkCopyColumnMappingCollection.cs
- ModelUIElement3D.cs
- SqlTypeSystemProvider.cs
- ObfuscationAttribute.cs
- DocumentGridContextMenu.cs
- UnhandledExceptionEventArgs.cs
- DateTimeOffsetAdapter.cs
- DoubleAnimationUsingKeyFrames.cs
- CommandLibraryHelper.cs
- TeredoHelper.cs
- PropertyCollection.cs
- ColumnResult.cs
- DragDrop.cs
- TemplateComponentConnector.cs
- MouseCaptureWithinProperty.cs
- ScriptHandlerFactory.cs
- ActivationServices.cs
- DataGridViewSelectedCellsAccessibleObject.cs
- RelationshipWrapper.cs
- ContainerAction.cs
- Point3DCollectionConverter.cs
- wgx_commands.cs
- BackoffTimeoutHelper.cs
- ToolboxDataAttribute.cs
- ToolStripHighContrastRenderer.cs
- WebMessageBodyStyleHelper.cs
- DataGridClipboardHelper.cs
- GlyphInfoList.cs
- RequestSecurityToken.cs
- ImageMap.cs
- XmlDataSourceNodeDescriptor.cs
- HtmlShim.cs
- QilList.cs
- ToolstripProfessionalRenderer.cs
- Serializer.cs
- ListViewDeletedEventArgs.cs