Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / 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
- MultiBinding.cs
- SurrogateEncoder.cs
- Point3DCollectionValueSerializer.cs
- PerformanceCounterLib.cs
- AutoFocusStyle.xaml.cs
- UsernameTokenFactoryCredential.cs
- EdmComplexTypeAttribute.cs
- ChangesetResponse.cs
- DataRowExtensions.cs
- ClientBuildManagerTypeDescriptionProviderBridge.cs
- VSWCFServiceContractGenerator.cs
- RegularExpressionValidator.cs
- AutoResizedEvent.cs
- UshortList2.cs
- FontNamesConverter.cs
- DelimitedListTraceListener.cs
- EDesignUtil.cs
- AttachmentCollection.cs
- HttpGetServerProtocol.cs
- WorkflowRequestContext.cs
- HttpListener.cs
- RepeatBehaviorConverter.cs
- FixedSOMPage.cs
- ServiceObjectContainer.cs
- ScaleTransform.cs
- RoleService.cs
- DynamicDataExtensions.cs
- localization.cs
- CompilerErrorCollection.cs
- AnimationException.cs
- CompositeDesignerAccessibleObject.cs
- PropertyKey.cs
- PolyQuadraticBezierSegment.cs
- CompositeDesignerAccessibleObject.cs
- XmlTextWriter.cs
- RegexRunnerFactory.cs
- Visual3D.cs
- ConfigurationLocationCollection.cs
- CodeDirectoryCompiler.cs
- ExternalFile.cs
- HtmlTextArea.cs
- ScalarConstant.cs
- Expression.cs
- CalculatedColumn.cs
- CngUIPolicy.cs
- DisplayNameAttribute.cs
- TargetFrameworkAttribute.cs
- IntSecurity.cs
- _ChunkParse.cs
- ResourceIDHelper.cs
- ContractMapping.cs
- DownloadProgressEventArgs.cs
- HttpProfileGroupBase.cs
- ExtendedPropertiesHandler.cs
- MetadataFile.cs
- MonikerUtility.cs
- GifBitmapDecoder.cs
- ExclusiveHandleList.cs
- RightsManagementEncryptedStream.cs
- UncommonField.cs
- JapaneseLunisolarCalendar.cs
- OleDbRowUpdatedEvent.cs
- XmlSecureResolver.cs
- IDQuery.cs
- FunctionParameter.cs
- DataGridRow.cs
- MimeMapping.cs
- SchemaReference.cs
- OutOfProcStateClientManager.cs
- CustomPopupPlacement.cs
- _NetRes.cs
- InvokeMethod.cs
- TreeViewImageGenerator.cs
- QilParameter.cs
- SiteMapPath.cs
- ChannelEndpointElement.cs
- Array.cs
- XmlCharacterData.cs
- DetailsView.cs
- invalidudtexception.cs
- SynchronousChannelMergeEnumerator.cs
- Function.cs
- FuncCompletionCallbackWrapper.cs
- TypeLibConverter.cs
- SmiContextFactory.cs
- WebServiceEnumData.cs
- MediaElement.cs
- PrincipalPermission.cs
- PathSegmentCollection.cs
- SqlUtil.cs
- MaskedTextBoxTextEditor.cs
- WebPartPersonalization.cs
- XmlNodeComparer.cs
- COM2FontConverter.cs
- ThreadStartException.cs
- TextBox.cs
- TextMarkerSource.cs
- GlyphsSerializer.cs
- HtmlInputCheckBox.cs
- OracleBoolean.cs