Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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
/** @attribute ComVisibleAttribute(true) */ 
public class TypeConverter
ComVisibleAttribute(true) 
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.

NoteNote:

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

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

NoteNote:

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.
NoteNote:

The HostProtectionAttribute attribute applied to this class has the following Resources property value: SharedState. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

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(typeof(MyClassConverter))]
 public class MyClass {
    // Insert code here.
}

/** @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 MyPropertyEnum MyProperty {
    set {
       // Checks to see if the value passed is valid.
       if (!TypeDescriptor.GetConverter(typeof(MyPropertyEnum)).IsValid(value)) {
          throw new ArgumentException();
       }
       // The value is valid. Insert code to set the 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.

Color c = Color.Red;
    Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c));

Color c = Color.get_Red();
Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c));

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

Color c = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString("Red");

Color c = (Color)(TypeDescriptor.GetConverter(Color.class.ToType()).
    ConvertFromString("Red"));

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

foreach(Color c in TypeDescriptor.GetConverter(typeof(Color)).GetStandardValues()) {
    Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c));
 }

IEnumerator e = TypeDescriptor.GetConverter(Color.class.ToType()).
    GetStandardValues().GetEnumerator();
while (e.MoveNext()) {
    Color c = (Color)e.get_Current();
    Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c));
}

System.Object
  System.ComponentModel.TypeConverter
     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

Community Additions

Show:
© 2014 Microsoft. All rights reserved.