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
- Item.cs
- NativeObjectSecurity.cs
- RadialGradientBrush.cs
- XmlExtensionFunction.cs
- SystemWebCachingSectionGroup.cs
- SecurityToken.cs
- HttpListenerElement.cs
- DataGridViewAutoSizeColumnModeEventArgs.cs
- DataGridView.cs
- XsltOutput.cs
- ComponentFactoryHelpers.cs
- CompilerParameters.cs
- HtmlInputRadioButton.cs
- XmlStreamStore.cs
- NumberFormatter.cs
- BodyGlyph.cs
- CodeNamespaceCollection.cs
- FileDetails.cs
- ObjectFullSpanRewriter.cs
- ElementUtil.cs
- LineSegment.cs
- DataGridTableCollection.cs
- InternalConfigHost.cs
- Span.cs
- PerformanceCounterCategory.cs
- cookie.cs
- IndicShape.cs
- Grant.cs
- StringKeyFrameCollection.cs
- xmlfixedPageInfo.cs
- RegistryPermission.cs
- AnnotationAdorner.cs
- InvokePattern.cs
- PassportAuthenticationEventArgs.cs
- CodePropertyReferenceExpression.cs
- RtType.cs
- CompositeKey.cs
- BrowserCapabilitiesFactoryBase.cs
- TypeUsage.cs
- ClrProviderManifest.cs
- CacheHelper.cs
- ConfigurationManagerInternalFactory.cs
- SQLInt16.cs
- VirtualizedContainerService.cs
- ApplicationActivator.cs
- SqlVersion.cs
- OleDbError.cs
- ListViewHitTestInfo.cs
- ConstrainedDataObject.cs
- DropSourceBehavior.cs
- SafeFindHandle.cs
- LabelTarget.cs
- Attributes.cs
- HtmlMobileTextWriter.cs
- ToolStripCodeDomSerializer.cs
- TypeValidationEventArgs.cs
- Tracking.cs
- UIElementAutomationPeer.cs
- PinnedBufferMemoryStream.cs
- TailCallAnalyzer.cs
- EventWaitHandle.cs
- ToolStripDropDownItem.cs
- DbConnectionPoolOptions.cs
- ResourceKey.cs
- ListControlBuilder.cs
- TargetPerspective.cs
- FormatException.cs
- InvokePattern.cs
- PropertyStore.cs
- MemberListBinding.cs
- ResourcesBuildProvider.cs
- ObjectPersistData.cs
- DataList.cs
- TypeFieldSchema.cs
- SqlBuffer.cs
- KeyNotFoundException.cs
- OleDbPermission.cs
- FlowDocumentScrollViewer.cs
- XmlCountingReader.cs
- AggregateNode.cs
- ReliableDuplexSessionChannel.cs
- PseudoWebRequest.cs
- DataError.cs
- KeyValuePair.cs
- LicenseManager.cs
- UrlAuthorizationModule.cs
- DataGridViewBindingCompleteEventArgs.cs
- LicenseProviderAttribute.cs
- XmlChildEnumerator.cs
- KnownBoxes.cs
- WebPartChrome.cs
- DataGridItem.cs
- ObfuscationAttribute.cs
- SQLInt16.cs
- CachedCompositeFamily.cs
- TempFiles.cs
- StreamingContext.cs
- OleDbParameter.cs
- EdgeProfileValidation.cs
- HtmlInputSubmit.cs