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
- ModuleBuilderData.cs
- XmlDownloadManager.cs
- AttributeParameterInfo.cs
- RootDesignerSerializerAttribute.cs
- SqlProcedureAttribute.cs
- RuleInfoComparer.cs
- FieldDescriptor.cs
- TextEndOfSegment.cs
- IntegerValidator.cs
- TextWriter.cs
- OneToOneMappingSerializer.cs
- WebPartDisplayMode.cs
- CustomCategoryAttribute.cs
- ImageMap.cs
- TrackingMemoryStreamFactory.cs
- GB18030Encoding.cs
- RuleSettings.cs
- ContentElement.cs
- DataGridViewAutoSizeColumnsModeEventArgs.cs
- WebConfigurationFileMap.cs
- VBIdentifierName.cs
- DbRetry.cs
- BaseDataListActionList.cs
- PathTooLongException.cs
- DataGridPagingPage.cs
- AuthenticationModulesSection.cs
- CompatibleIComparer.cs
- MappingItemCollection.cs
- TraceLevelStore.cs
- DNS.cs
- SizeIndependentAnimationStorage.cs
- TextDecoration.cs
- SafeHGlobalHandleCritical.cs
- WorkflowOperationContext.cs
- Rules.cs
- ControlHelper.cs
- TextParaLineResult.cs
- JoinGraph.cs
- LayoutDump.cs
- Array.cs
- DataGridTextBoxColumn.cs
- SqlConnectionPoolGroupProviderInfo.cs
- DataGridCell.cs
- ParameterInfo.cs
- QueryStatement.cs
- _NegotiateClient.cs
- XPathDocumentIterator.cs
- MessageSmuggler.cs
- DaylightTime.cs
- DataFormats.cs
- ListDictionary.cs
- SqlCacheDependency.cs
- ComplexObject.cs
- DataTable.cs
- DurableEnlistmentState.cs
- LineUtil.cs
- ValidationErrorEventArgs.cs
- GuidelineSet.cs
- LowerCaseStringConverter.cs
- XmlNodeList.cs
- UInt16.cs
- Int32Rect.cs
- InputScope.cs
- UpWmlPageAdapter.cs
- RtfFormatStack.cs
- Errors.cs
- CachedTypeface.cs
- CollectionConverter.cs
- GcHandle.cs
- SimpleTypesSurrogate.cs
- WebConfigurationHostFileChange.cs
- MemberHolder.cs
- SecureUICommand.cs
- ContextMenuAutomationPeer.cs
- SymLanguageType.cs
- ComponentSerializationService.cs
- TabControl.cs
- PtsCache.cs
- HttpListenerElement.cs
- SymmetricKeyWrap.cs
- LabelAutomationPeer.cs
- UniqueCodeIdentifierScope.cs
- StringArrayEditor.cs
- RadioButton.cs
- _AcceptOverlappedAsyncResult.cs
- DataGridColumn.cs
- DefaultIfEmptyQueryOperator.cs
- RoleService.cs
- TextRunCache.cs
- CommandField.cs
- FileRecordSequence.cs
- VariableQuery.cs
- GetWinFXPath.cs
- ValueTypeFieldReference.cs
- GeneralTransform.cs
- WebPartRestoreVerb.cs
- DesigntimeLicenseContext.cs
- CTreeGenerator.cs
- MetabaseServerConfig.cs
- TriggerAction.cs