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
- Shared.cs
- ToolStripDropDownButton.cs
- TransformationRules.cs
- SemaphoreFullException.cs
- RootBrowserWindowAutomationPeer.cs
- ProtectedProviderSettings.cs
- SecurityKeyIdentifierClause.cs
- IDReferencePropertyAttribute.cs
- DataSourceSelectArguments.cs
- BitmapFrame.cs
- SerialPort.cs
- MarkupExtensionParser.cs
- Control.cs
- BaseConfigurationRecord.cs
- OdbcTransaction.cs
- JsonCollectionDataContract.cs
- DataSourceHelper.cs
- SerializerProvider.cs
- RelativeSource.cs
- FileIOPermission.cs
- SystemFonts.cs
- PassportAuthenticationModule.cs
- ResolvedKeyFrameEntry.cs
- SID.cs
- KeyInfo.cs
- XmlCustomFormatter.cs
- JobDuplex.cs
- TypeValidationEventArgs.cs
- ItemList.cs
- ToolStripManager.cs
- TagMapInfo.cs
- shaper.cs
- SqlAliaser.cs
- RecognizeCompletedEventArgs.cs
- InlineObject.cs
- CompatibleComparer.cs
- DataGridViewLinkCell.cs
- DataColumnMappingCollection.cs
- OperationCanceledException.cs
- SvcMapFileSerializer.cs
- SQLInt16Storage.cs
- SettingsProviderCollection.cs
- LinqDataSourceContextEventArgs.cs
- TimeSpanSecondsOrInfiniteConverter.cs
- ShapingEngine.cs
- XamlFigureLengthSerializer.cs
- PolyQuadraticBezierSegment.cs
- xsdvalidator.cs
- Formatter.cs
- ConsoleCancelEventArgs.cs
- BookmarkEventArgs.cs
- ParentQuery.cs
- Transform3D.cs
- ResponseStream.cs
- ProfileParameter.cs
- TypeNameHelper.cs
- ExecutionContext.cs
- DbConnectionPoolGroupProviderInfo.cs
- DiffuseMaterial.cs
- RemotingSurrogateSelector.cs
- FaultPropagationRecord.cs
- PointLight.cs
- Image.cs
- TraceListener.cs
- DataGridViewHitTestInfo.cs
- DataGridViewCheckBoxCell.cs
- ShimAsPublicXamlType.cs
- securitycriticaldataformultiplegetandset.cs
- ScriptModule.cs
- UnknownWrapper.cs
- _ConnectStream.cs
- WindowsSpinner.cs
- SessionPageStateSection.cs
- FirewallWrapper.cs
- VisualStyleElement.cs
- WindowsGraphics.cs
- PrintPreviewControl.cs
- Win32Exception.cs
- Globals.cs
- PropertyBuilder.cs
- CompModSwitches.cs
- ThreadAbortException.cs
- Separator.cs
- WebPartTransformer.cs
- TextFragmentEngine.cs
- Lock.cs
- OleDbRowUpdatedEvent.cs
- SystemGatewayIPAddressInformation.cs
- EntityViewGenerationAttribute.cs
- AppDomainResourcePerfCounters.cs
- FileAuthorizationModule.cs
- SoapProcessingBehavior.cs
- BindingMemberInfo.cs
- xml.cs
- SizeChangedEventArgs.cs
- ResolvedKeyFrameEntry.cs
- RequestUriProcessor.cs
- TextBlockAutomationPeer.cs
- DetailsViewDeletedEventArgs.cs
- InvariantComparer.cs