EnumConverter Class

Definition

Provides a type converter to convert Enum objects to and from various other representations.

public class EnumConverter : System.ComponentModel.TypeConverter
Inheritance
EnumConverter
Derived

Inherited Members

System.ComponentModel.TypeConverter

System.Object

Examples

The following code example converts a variable of type Enum to a string, and vice versa. The example requires that you have declared an Enum called Servers and that it has the following members:

Windows=1, Exchange=2, BizTalk=3  
// Requires public declaration of the following type.
// __value enum Servers {Windows=1, Exchange=2, BizTalk=3};
Servers myServer = Servers::Exchange;
String^ myServerString = "BizTalk";
Console::WriteLine( TypeDescriptor::GetConverter( myServer )->ConvertTo( myServer, String::typeid ) );
Console::WriteLine( TypeDescriptor::GetConverter( myServer )->ConvertFrom( myServerString ) );
Enum myServer= Servers.Exchange;
string myServerString = "BizTalk";
Console.WriteLine(TypeDescriptor.GetConverter(myServer).ConvertTo(myServer, typeof(string))); 
Console.WriteLine(TypeDescriptor.GetConverter(myServer).ConvertFrom(myServerString));    

Dim myServer As Servers = Servers.Exchange
Dim myServerString As string = "BizTalk"
Console.WriteLine(TypeDescriptor.GetConverter(myServer).ConvertTo(myServer, GetType(String))) 
Console.WriteLine(TypeDescriptor.GetConverter(myServer).ConvertFrom(myServerString))	
			

Remarks

This converter can only convert an enumeration object to and from a string. The EnumConverter class provides the Comparer property to get an IComparer interface that can be used to sort the values of the enumeration. By default, the enumeration values are sorted in the order they appear in the file.

For more information about type converters, see the TypeConverter base class and How to: Implement a Type Converter.

Note

You should never create an instance of an EnumConverter. Instead, call the GetConverter method of the TypeDescriptor class. For more information, see the examples in the TypeConverter base class.

Constructors

EnumConverter(Type)

Initializes a new instance of the EnumConverter class for the given type.

Properties

Comparer

Gets an IComparer that can be used to sort the values of the enumeration.

EnumType

Specifies the type of the enumerator this converter is associated with.

Values

Gets or sets a TypeConverter.StandardValuesCollection that specifies the possible values for the enumeration.

Methods

CanConvertFrom(ITypeDescriptorContext, Type)

Gets a value indicating whether this converter can convert an object in the given source type to an enumeration object using the specified context.

CanConvertTo(ITypeDescriptorContext, Type)

Gets a value indicating whether this converter can convert an object to the given destination type using the context.

ConvertFrom(ITypeDescriptorContext, CultureInfo, Object)

Converts the specified value object to an enumeration object.

ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

Converts the given value object to the specified destination type.

GetStandardValues(ITypeDescriptorContext)

Gets a collection of standard values for the data type this validator is designed for.

GetStandardValuesExclusive(ITypeDescriptorContext)

Gets a value indicating whether the list of standard values returned from GetStandardValues() is an exclusive list using the specified context.

GetStandardValuesSupported(ITypeDescriptorContext)

Gets a value indicating whether this object supports a standard set of values that can be picked from a list using the specified context.

IsValid(ITypeDescriptorContext, Object)

Gets a value indicating whether the given object value is valid for this type.