This documentation is archived and is not being maintained.

TypeConverter Class

Provides a unified way of converting types of values to other types, as well as for accessing standard values and subproperties.

Namespace: System.ComponentModel
Assembly: System (in system.dll)

<ComVisibleAttribute(True)> _
Public Class TypeConverter
Dim instance As TypeConverter

/** @attribute ComVisibleAttribute(true) */ 
public class TypeConverter
public class TypeConverter
Not applicable.

The most common type of converter is one that converts to and from a text representation. The type converter for a class is bound to the class with a TypeConverterAttribute. Unless this attribute is overridden, all classes that inherit from this class use the same type converter as the base class.


Never access a type converter directly. Instead, access the appropriate converter by using TypeDescriptor. For more information, see the code examples provided.


In Windows Presentation Foundation (WPF), the XAML loader searches for the TypeConverterAttribute directly, instead of going through TypeDescriptor.

Notes to Inheritors: Inherit from TypeConverter to implement your own conversion requirements. When you inherit from this class, you can override the following methods:


Your derived type might be marked as internal or private, but an instance of your type can be created with the TypeDescriptor class. Do not write insecure code by assuming the caller is trusted. Assume instead that callers might create instances of your type in partial trust.

For more information about type converters, see How to: Implement a Type Converter or Generalized Type Conversion.

The following code example shows how to create an instance of a type converter and bind it to a class. The class implementing the converter, MyClassConverter, must inherit from the TypeConverter class.

<TypeConverter(GetType(MyClassConverter))> _
Public Class Class1
    ' Insert code here.
End Class 'MyClass

/** @attribute TypeConverter(MyClassConverter.class)
public class MyClass
    // Insert code here.
} //MyClass 

When you have a property that has an enumeration, check to see whether an enumeration value is valid before setting the property. The next code example requires that an enumeration called MyPropertyEnum has been declared.

Public WriteOnly Property MyProperty() As MyPropertyEnum
        ' Checks to see if the value passed is valid.
        If Not TypeDescriptor.GetConverter(GetType(MyPropertyEnum)).IsValid(value) Then
            Throw New ArgumentException()
        End If
        ' The value is valid. Insert code to set the property.
    End Set 
End Property

/** @property 
public void set_MyProperty(MyPropertyEnum value)
    // Checks to see if the value passed is valid.
    if (!(TypeDescriptor.GetConverter(MyPropertyEnum.class.ToType()).
        IsValid(value))) {
        throw new ArgumentException();
    // The value is valid. Insert code to set the property.
} //set_MyProperty

Another common type converter usage is to convert an object to a string. The following code example prints out the name of the Color stored in the variable c.

Dim c As Color = Color.Red

Color c = Color.get_Red();

You can also use a type converter to convert a value from its name, as shown in the next code example.

Dim c As Color = CType(TypeDescriptor.GetConverter(GetType(Color)).ConvertFromString("Red"), Color)

Color c = (Color)(TypeDescriptor.GetConverter(Color.class.ToType()).

In the following code example, you can use a type converter to print out the set of standard values that the object supports.

Dim c As Color
For Each c In  TypeDescriptor.GetConverter(GetType(Color)).GetStandardValues()
Next c

IEnumerator e = TypeDescriptor.GetConverter(Color.class.ToType()).
while (e.MoveNext()) {
    Color c = (Color)e.get_Current();

     Derived Classes

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0