Provides a unified way of converting types of values to other types, as well as for accessing standard values and subproperties.
For a list of all members of this type, see TypeConverter Members.
System.Object
System.ComponentModel.TypeConverter
Derived classes
[Visual Basic] <ComVisible(True)> Public Class TypeConverter [C#] [ComVisible(true)] public class TypeConverter [C++] [ComVisible(true)] public __gc class TypeConverter [JScript] public ComVisible(true) class TypeConverter
Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.
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.
Note 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:
For more information about type converters, see
[Visual Basic, C#] The following 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.
[Visual Basic] <TypeConverter(GetType(MyClassConverter))> _ Public Class Class1 ' Insert code here. End Class 'MyClass [C#] [TypeConverter(typeof(MyClassConverter))] public class MyClass { // Insert code here. }
[Visual Basic, C#] 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 assumes that an enumeration called MyPropertyEnum has been declared.
[Visual Basic] Public WriteOnly Property MyProperty() As MyPropertyEnum Set ' 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 [C#] 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. } }
[Visual Basic, C#] Another common type converter usage is to convert an object to a string. The following example prints out the name of the Color stored in the variable c.
[Visual Basic] Dim c As Color = Color.Red Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c)) [C#] Color c = Color.Red; Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c));
[Visual Basic, C#] You can also use a type converter to convert a value from its name, as shown in the next code example.
[Visual Basic] Dim c As Color = CType(TypeDescriptor.GetConverter(GetType(Color)).ConvertFromString("Red"), Color) [C#] Color c = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString("Red");
[Visual Basic, C#] In this example, you can use a type converter to print out the set of standard values that the object supports.
[Visual Basic] Dim c As Color For Each c In TypeDescriptor.GetConverter(GetType(Color)).GetStandardValues() Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c)) Next c [C#] foreach(Color c in TypeDescriptor.GetConverter(typeof(Color)).GetStandardValues()) { Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c)); }
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Namespace: System.ComponentModel
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework - Windows CE .NET
Assembly: System (in System.dll)
TypeConverter Members | System.ComponentModel Namespace | TypeConverterAttribute | PropertyDescriptorCollection | TypeConverter.SimplePropertyDescriptor | TypeConverter.StandardValuesCollection