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
- AutomationIdentifier.cs
- DrawingContextWalker.cs
- ListViewGroupItemCollection.cs
- Exceptions.cs
- EmbeddedMailObjectsCollection.cs
- DesignerCategoryAttribute.cs
- Overlapped.cs
- WebAdminConfigurationHelper.cs
- PeerObject.cs
- ToolStripSplitButton.cs
- WebServiceTypeData.cs
- EventMappingSettings.cs
- IPGlobalProperties.cs
- ErrorTolerantObjectWriter.cs
- PropertyItem.cs
- EraserBehavior.cs
- DoWorkEventArgs.cs
- LinqDataSourceInsertEventArgs.cs
- DictionaryCustomTypeDescriptor.cs
- KeyEvent.cs
- UdpReplyToBehavior.cs
- DrawingVisual.cs
- SpeechEvent.cs
- FontCollection.cs
- ParameterCollection.cs
- ValueQuery.cs
- CharStorage.cs
- ReliabilityContractAttribute.cs
- SerTrace.cs
- XmlSchemaNotation.cs
- SoapFault.cs
- RedistVersionInfo.cs
- HtmlTernaryTree.cs
- ConfigurationElementProperty.cs
- MultiByteCodec.cs
- SimpleTypeResolver.cs
- UInt32Converter.cs
- PanelDesigner.cs
- RuntimeHandles.cs
- LayoutEditorPart.cs
- BitmapEffectDrawingContent.cs
- ExpandableObjectConverter.cs
- ObjectAssociationEndMapping.cs
- DataControlHelper.cs
- XPathNavigatorReader.cs
- VirtualDirectoryMapping.cs
- RegexGroupCollection.cs
- BufferModeSettings.cs
- ResourcePermissionBase.cs
- CryptoApi.cs
- AssociationSet.cs
- _NtlmClient.cs
- InternalException.cs
- UnhandledExceptionEventArgs.cs
- QueryableDataSourceView.cs
- DesignerForm.cs
- _RequestCacheProtocol.cs
- Int64Converter.cs
- SystemThemeKey.cs
- OracleFactory.cs
- ChangePassword.cs
- ModelServiceImpl.cs
- TcpHostedTransportConfiguration.cs
- BigInt.cs
- PartBasedPackageProperties.cs
- SurrogateEncoder.cs
- AQNBuilder.cs
- ShaderEffect.cs
- EncryptedType.cs
- _FtpControlStream.cs
- GZipDecoder.cs
- HttpCapabilitiesEvaluator.cs
- LineGeometry.cs
- WebServicesDescriptionAttribute.cs
- DataGridViewRowHeightInfoNeededEventArgs.cs
- ECDsa.cs
- DataGridViewColumnCollection.cs
- RuleSettings.cs
- NullableDoubleMinMaxAggregationOperator.cs
- DetailsViewInsertEventArgs.cs
- GroupDescription.cs
- CompareInfo.cs
- Debug.cs
- WebPartAddingEventArgs.cs
- XmlHierarchyData.cs
- ScriptingSectionGroup.cs
- InternalSafeNativeMethods.cs
- DataServiceProcessingPipelineEventArgs.cs
- DesigntimeLicenseContextSerializer.cs
- FileDialogCustomPlaces.cs
- SspiSafeHandles.cs
- NavigationWindowAutomationPeer.cs
- IfAction.cs
- OptimizerPatterns.cs
- Ticks.cs
- ConfigDefinitionUpdates.cs
- ChannelManager.cs
- RSAPKCS1SignatureDeformatter.cs
- XmlSchemaNotation.cs
- NaturalLanguageHyphenator.cs