Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / 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
- ToolStripOverflow.cs
- ApplicationDirectoryMembershipCondition.cs
- MemberHolder.cs
- ListBoxItem.cs
- ArrayHelper.cs
- MethodCallConverter.cs
- TypedTableGenerator.cs
- MouseOverProperty.cs
- ACE.cs
- MissingMethodException.cs
- MergePropertyDescriptor.cs
- BaseCodeDomTreeGenerator.cs
- WebBrowsableAttribute.cs
- TextTreeInsertElementUndoUnit.cs
- TTSEngineProxy.cs
- ObjectSet.cs
- XomlCompilerError.cs
- MetadataArtifactLoaderCompositeResource.cs
- EmptyControlCollection.cs
- InfoCardRequestException.cs
- MessageFilterTable.cs
- XmlSignatureProperties.cs
- ErrorFormatter.cs
- XmlSchemaProviderAttribute.cs
- MetadataCacheItem.cs
- FileVersion.cs
- SystemColors.cs
- codemethodreferenceexpression.cs
- PropagatorResult.cs
- Boolean.cs
- DeferrableContent.cs
- _BaseOverlappedAsyncResult.cs
- InternalControlCollection.cs
- ChainOfDependencies.cs
- AdornerLayer.cs
- AutoGeneratedField.cs
- ReferencedType.cs
- ColumnMapProcessor.cs
- MetadataItemCollectionFactory.cs
- VectorCollectionValueSerializer.cs
- ProjectionCamera.cs
- Variable.cs
- IPPacketInformation.cs
- KeyConstraint.cs
- DataGridItem.cs
- ImmutableObjectAttribute.cs
- SafePointer.cs
- FontStyleConverter.cs
- ScriptReference.cs
- grammarelement.cs
- PrimaryKeyTypeConverter.cs
- PingOptions.cs
- LineInfo.cs
- FormViewInsertEventArgs.cs
- xmlfixedPageInfo.cs
- RemoteWebConfigurationHostStream.cs
- MessageFilter.cs
- ListViewItemSelectionChangedEvent.cs
- SerializationException.cs
- DependencyPropertyKind.cs
- RemotingConfiguration.cs
- StylusPointProperties.cs
- OptimalBreakSession.cs
- BatchServiceHost.cs
- MetroSerializationManager.cs
- ObjectToIdCache.cs
- Select.cs
- AdapterUtil.cs
- Parameter.cs
- HashHelper.cs
- CodeDomConfigurationHandler.cs
- ThreadExceptionEvent.cs
- CroppedBitmap.cs
- RestClientProxyHandler.cs
- TextStore.cs
- WhiteSpaceTrimStringConverter.cs
- SignedPkcs7.cs
- DeviceFilterEditorDialog.cs
- FilteredDataSetHelper.cs
- OverflowException.cs
- MenuAdapter.cs
- IdentifierCollection.cs
- XPathEmptyIterator.cs
- DataStreamFromComStream.cs
- DefaultTextStoreTextComposition.cs
- safePerfProviderHandle.cs
- HttpProfileBase.cs
- SizeAnimationBase.cs
- EndpointAddressElementBase.cs
- PropertyOverridesDialog.cs
- NameValueFileSectionHandler.cs
- ConnectionPoint.cs
- Utils.cs
- Listbox.cs
- ElementHostAutomationPeer.cs
- ContractCodeDomInfo.cs
- SessionStateModule.cs
- AspNetHostingPermission.cs
- UnionExpr.cs
- BaseTemplateCodeDomTreeGenerator.cs