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
![Network programming in C#, Network Programming in VB.NET, Network Programming in .NET](/images/book.jpg)
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- WrappedIUnknown.cs
- SamlConstants.cs
- ConfigurationProperty.cs
- SafeTokenHandle.cs
- TextParentUndoUnit.cs
- NullableBoolConverter.cs
- TimeSpanStorage.cs
- Roles.cs
- ArrayConverter.cs
- MarkupWriter.cs
- ContractUtils.cs
- EventLogRecord.cs
- RuntimeHandles.cs
- HttpPostedFileWrapper.cs
- XmlStreamedByteStreamReader.cs
- SortExpressionBuilder.cs
- CompressedStack.cs
- CodeIndexerExpression.cs
- ApplicationBuildProvider.cs
- ByteStorage.cs
- C14NUtil.cs
- ErrorView.xaml.cs
- OleDbEnumerator.cs
- StoreAnnotationsMap.cs
- SqlXml.cs
- UnsafeNativeMethods.cs
- UnionCodeGroup.cs
- CollectionAdapters.cs
- WebPartUserCapability.cs
- RegexCode.cs
- CustomSignedXml.cs
- StateChangeEvent.cs
- FixedPageStructure.cs
- SpellerHighlightLayer.cs
- UrlRoutingModule.cs
- HtmlUtf8RawTextWriter.cs
- AutomationPattern.cs
- Matrix3D.cs
- Literal.cs
- MessageQueueException.cs
- _NestedSingleAsyncResult.cs
- JournalEntryStack.cs
- DataGridViewSortCompareEventArgs.cs
- DispatcherExceptionEventArgs.cs
- HtmlLiteralTextAdapter.cs
- DbDataAdapter.cs
- HiddenFieldPageStatePersister.cs
- DataServiceRequestOfT.cs
- ProfileInfo.cs
- ErrorCodes.cs
- ExtentJoinTreeNode.cs
- ImageSource.cs
- FontStretchConverter.cs
- AsyncContentLoadedEventArgs.cs
- TreeNodeMouseHoverEvent.cs
- FrameworkContentElement.cs
- WebPartExportVerb.cs
- SqlCachedBuffer.cs
- LongValidator.cs
- DBPropSet.cs
- ProxyWebPart.cs
- Funcletizer.cs
- AuthorizationContext.cs
- cache.cs
- DataBindingCollection.cs
- CompressionTracing.cs
- SectionRecord.cs
- ProvideValueServiceProvider.cs
- ExpressionBuilderCollection.cs
- Contracts.cs
- SafeFileMappingHandle.cs
- DrawingBrush.cs
- SerialErrors.cs
- CapabilitiesPattern.cs
- AxisAngleRotation3D.cs
- SignatureTargetIdManager.cs
- HTTPRemotingHandler.cs
- Int32.cs
- ArrayList.cs
- ListBoxChrome.cs
- Site.cs
- MetadataUtil.cs
- Double.cs
- RowCache.cs
- HtmlGenericControl.cs
- UICuesEvent.cs
- SafeBuffer.cs
- DocumentGrid.cs
- WebServiceAttribute.cs
- SubMenuStyleCollection.cs
- TemplatePagerField.cs
- ThemeConfigurationDialog.cs
- sqlinternaltransaction.cs
- TemplateKey.cs
- TextEvent.cs
- DbDataRecord.cs
- NavigationExpr.cs
- AffineTransform3D.cs
- FieldToken.cs
- CharEntityEncoderFallback.cs