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
- AsyncSerializedWorker.cs
- BulletedListEventArgs.cs
- BatchParser.cs
- DecoderExceptionFallback.cs
- GuidTagList.cs
- ExtractedStateEntry.cs
- CatalogPartCollection.cs
- GroupAggregateExpr.cs
- WsatConfiguration.cs
- QilVisitor.cs
- BoundColumn.cs
- SimpleHandlerBuildProvider.cs
- DefaultShape.cs
- DrawingCollection.cs
- NetWebProxyFinder.cs
- Relationship.cs
- SendActivityEventArgs.cs
- SqlNodeAnnotation.cs
- TileBrush.cs
- MatrixConverter.cs
- Unit.cs
- XmlIlVisitor.cs
- XsdCachingReader.cs
- ServiceDocument.cs
- ScrollableControl.cs
- DrawItemEvent.cs
- Vector3dCollection.cs
- OracleConnectionStringBuilder.cs
- RootProfilePropertySettingsCollection.cs
- SQLDateTime.cs
- Condition.cs
- SQLConvert.cs
- SqlAggregateChecker.cs
- BinHexEncoding.cs
- UnmanagedBitmapWrapper.cs
- MachineKeyConverter.cs
- DbConnectionPoolGroup.cs
- UIElementHelper.cs
- AppSettingsExpressionBuilder.cs
- WebResourceAttribute.cs
- TextSimpleMarkerProperties.cs
- HtmlControlPersistable.cs
- RequestUriProcessor.cs
- FrameworkObject.cs
- XamlTypeWithExplicitNamespace.cs
- MethodBuilder.cs
- Cursor.cs
- MSHTMLHost.cs
- EmbeddedMailObject.cs
- HttpProfileBase.cs
- HiddenField.cs
- DataGridViewImageColumn.cs
- MasterPageParser.cs
- TemplatePropertyEntry.cs
- DragDrop.cs
- TextBox.cs
- ModuleBuilder.cs
- regiisutil.cs
- TableProviderWrapper.cs
- Dictionary.cs
- SpecularMaterial.cs
- IdentifierCollection.cs
- StringComparer.cs
- DataControlReference.cs
- EditingMode.cs
- InheritedPropertyDescriptor.cs
- UntypedNullExpression.cs
- MyContact.cs
- TableRowCollection.cs
- Vector3D.cs
- ViewGenerator.cs
- DateTimePicker.cs
- StatusBarItem.cs
- ManipulationDelta.cs
- ConnectionsZoneDesigner.cs
- ColumnWidthChangedEvent.cs
- FrameworkElementFactory.cs
- DataGridViewComboBoxColumnDesigner.cs
- X509CertificateCollection.cs
- MeshGeometry3D.cs
- ImageFormat.cs
- ToolStripDropDownClosedEventArgs.cs
- CommonXSendMessage.cs
- BehaviorEditorPart.cs
- LeftCellWrapper.cs
- Convert.cs
- NamespaceList.cs
- ItemCheckedEvent.cs
- DifferencingCollection.cs
- BackStopAuthenticationModule.cs
- FtpCachePolicyElement.cs
- Console.cs
- QilList.cs
- StaticContext.cs
- HeaderUtility.cs
- GB18030Encoding.cs
- LinkUtilities.cs
- TTSEngineProxy.cs
- TypeUtil.cs
- OneOfElement.cs