Export (0) Print
Expand All

TypeConverter Class

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

Thread Safety

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

Remarks

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 Implementing a Type Converter or Generalized Type Conversion.

Example

[Visual Basic, C#, 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.
}

[C++] 
public:
    [TypeConverter(__typeof(Sample::MyClassConverter))]
     __gc class MyClass {
        // Insert code here.
    };

[Visual Basic, C#, 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.
    }
 }

[C++] 
public:
    __property void set_MyProperty( MyPropertyEnum value ) {
           // Checks to see if the value passed is valid.
           if (!TypeDescriptor::GetConverter(__typeof(MyPropertyEnum))->IsValid(__box(value))) {
              throw new ArgumentException();
           }
           // The value is valid. Insert code to set the property.
        }
     

[Visual Basic, C#, 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));

[C++] 
Color c = Color::Red;
Console::WriteLine(TypeDescriptor::GetConverter( __box(c))->ConvertToString(__box(c)));

[Visual Basic, C#, 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");

[C++] 
Color c = *dynamic_cast<__box Color*>(TypeDescriptor::GetConverter(__typeof(Color))->ConvertFromString(S"Red"));

[Visual Basic, C#, 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++] 
System::Collections::IEnumerator* myEnum = TypeDescriptor::GetConverter(__typeof(Color))->GetStandardValues()->GetEnumerator();
while (myEnum->MoveNext())
{
    Color c = *__try_cast<__box Color*>(myEnum->Current);
    Console::WriteLine(TypeDescriptor::GetConverter( __box(c))->ConvertToString(__box(c)));
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

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

Assembly: System (in System.dll)

See Also

TypeConverter Members | System.ComponentModel Namespace | TypeConverterAttribute | PropertyDescriptorCollection | TypeConverter.SimplePropertyDescriptor | TypeConverter.StandardValuesCollection

Show:
© 2014 Microsoft