Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Configuration / System / Configuration / GenericEnumConverter.cs / 1305376 / 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
- SQLMoneyStorage.cs
- HtmlTable.cs
- ValidatedControlConverter.cs
- TableLayoutStyleCollection.cs
- RenderOptions.cs
- Light.cs
- ObjectStateFormatter.cs
- securitymgrsite.cs
- RtfToXamlLexer.cs
- PhysicalFontFamily.cs
- counter.cs
- GeneralTransformCollection.cs
- HierarchicalDataBoundControlAdapter.cs
- Group.cs
- ItemsPanelTemplate.cs
- ObjectTag.cs
- ToolBarButton.cs
- LicenseContext.cs
- KnownTypes.cs
- Currency.cs
- TypeGeneratedEventArgs.cs
- ProgressBarHighlightConverter.cs
- AddingNewEventArgs.cs
- LabelEditEvent.cs
- DataRow.cs
- DebugView.cs
- ISAPIWorkerRequest.cs
- SecurityDescriptor.cs
- ProtocolsConfiguration.cs
- QueryInterceptorAttribute.cs
- ResourceReader.cs
- WsdlBuildProvider.cs
- TemplateApplicationHelper.cs
- PixelFormatConverter.cs
- UrlMappingCollection.cs
- WebControlsSection.cs
- PathData.cs
- EntitySetBaseCollection.cs
- NullableDoubleAverageAggregationOperator.cs
- DurableRuntimeValidator.cs
- ServiceBusyException.cs
- IsolatedStorage.cs
- ConnectionPoolManager.cs
- RankException.cs
- TrackingSection.cs
- CodeTypeMemberCollection.cs
- ToolStripManager.cs
- CompositeActivityTypeDescriptorProvider.cs
- _HeaderInfoTable.cs
- ThemeInfoAttribute.cs
- ReceiveErrorHandling.cs
- RadioButtonList.cs
- ConstNode.cs
- CircleHotSpot.cs
- RegexMatchCollection.cs
- RelationshipConstraintValidator.cs
- SafeArchiveContext.cs
- XmlSchemaComplexType.cs
- BaseResourcesBuildProvider.cs
- XmlAttribute.cs
- PeerNearMe.cs
- VectorAnimation.cs
- DetailsViewCommandEventArgs.cs
- XmlExtensionFunction.cs
- ClientUtils.cs
- NamedPipeChannelListener.cs
- ValueQuery.cs
- SizeKeyFrameCollection.cs
- ObjectDataSourceEventArgs.cs
- XpsS0ValidatingLoader.cs
- CorrelationResolver.cs
- EdmMember.cs
- TemplateControlBuildProvider.cs
- QuaternionAnimation.cs
- hwndwrapper.cs
- CodeTypeReferenceCollection.cs
- BindingsCollection.cs
- AuthenticationServiceManager.cs
- DataServiceQueryException.cs
- OneToOneMappingSerializer.cs
- CrossAppDomainChannel.cs
- FloaterBaseParaClient.cs
- TableLayoutSettingsTypeConverter.cs
- FigureParagraph.cs
- SessionPageStateSection.cs
- DoubleCollection.cs
- CroppedBitmap.cs
- SRGSCompiler.cs
- EncryptedReference.cs
- TreeSet.cs
- NestedContainer.cs
- MessageRpc.cs
- ContainerAction.cs
- Brush.cs
- EastAsianLunisolarCalendar.cs
- KeyTime.cs
- HtmlTableRowCollection.cs
- XmlSerializationWriter.cs
- LightweightEntityWrapper.cs
- TemplatePartAttribute.cs