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
- KeysConverter.cs
- EntityStoreSchemaFilterEntry.cs
- ConfigXmlText.cs
- ExtendLockAsyncResult.cs
- ControlValuePropertyAttribute.cs
- Int32KeyFrameCollection.cs
- MexTcpBindingCollectionElement.cs
- EndpointInfo.cs
- RawStylusInput.cs
- PropertyManager.cs
- Events.cs
- GridViewDeleteEventArgs.cs
- XmlMembersMapping.cs
- COM2FontConverter.cs
- RepeaterItemCollection.cs
- Error.cs
- DependencyPropertyHelper.cs
- WebPartConnectionsEventArgs.cs
- safelinkcollection.cs
- SqlNodeTypeOperators.cs
- XmlUtilWriter.cs
- ToolStripDropDownClosingEventArgs.cs
- SafeNativeMethods.cs
- IDispatchConstantAttribute.cs
- StringComparer.cs
- ListBox.cs
- Tuple.cs
- SqlConnectionFactory.cs
- TemplateControl.cs
- TimeSpan.cs
- XmlQueryTypeFactory.cs
- ObjectDataSourceDisposingEventArgs.cs
- XmlSchemaImporter.cs
- ConstructorArgumentAttribute.cs
- SelectQueryOperator.cs
- PlainXmlWriter.cs
- CoreSwitches.cs
- ActiveXHelper.cs
- TrackBarRenderer.cs
- ConnectionString.cs
- AccessText.cs
- ExpressionPrinter.cs
- SwitchLevelAttribute.cs
- WriterOutput.cs
- ProxySimple.cs
- TextTreeExtractElementUndoUnit.cs
- AsyncStreamReader.cs
- RegexInterpreter.cs
- HashHelper.cs
- XmlTextReaderImplHelpers.cs
- XmlSerializer.cs
- ClientTargetSection.cs
- TableDetailsCollection.cs
- EdmItemCollection.cs
- IPHostEntry.cs
- XmlHierarchyData.cs
- ArrayWithOffset.cs
- MeasurementDCInfo.cs
- TrustLevelCollection.cs
- ExpressionBuilderCollection.cs
- DoubleAnimationUsingKeyFrames.cs
- CommonGetThemePartSize.cs
- DataRelationPropertyDescriptor.cs
- SqlStatistics.cs
- AddInIpcChannel.cs
- InvalidCommandTreeException.cs
- LocationChangedEventArgs.cs
- TemplateControlBuildProvider.cs
- X509RawDataKeyIdentifierClause.cs
- AddingNewEventArgs.cs
- XPathNodePointer.cs
- EntityProxyTypeInfo.cs
- XmlSchemaException.cs
- TypeValidationEventArgs.cs
- TransformationRules.cs
- TraceHandlerErrorFormatter.cs
- SafeMILHandle.cs
- MailMessage.cs
- ArraySortHelper.cs
- UInt32.cs
- ExpressionBuilderCollection.cs
- ErrorTableItemStyle.cs
- AssociatedControlConverter.cs
- ExpressionHelper.cs
- MemberHolder.cs
- DataGridViewCellLinkedList.cs
- WindowShowOrOpenTracker.cs
- ObjectResult.cs
- SoapIgnoreAttribute.cs
- SQLInt64Storage.cs
- IFlowDocumentViewer.cs
- EmptyQuery.cs
- DataObjectCopyingEventArgs.cs
- IndexedString.cs
- DataGridViewSelectedRowCollection.cs
- TraceUtils.cs
- unitconverter.cs
- UrlMappingsSection.cs
- TextDpi.cs
- HttpRuntimeSection.cs