Generalized Type Conversion
The .NET Framework provides the following two mechanisms for converting user-defined data types (custom types) to other data types:
Defining a type converter for a custom type by extending the System.ComponentModel.TypeConverter class and associating the type converter with the type through a System.ComponentModel.TypeConverterAttribute attribute.
Implementing the System.IConvertible interface on a custom type.
The following table highlights the differences between these two mechanisms.
Note
Design-time support can be provided for a custom type only if it has a type converter defined for it.
Conversion using TypeConverter |
Conversion using IConvertible |
---|---|
Can be used both at design time and at run time. |
Can be used only at run time. |
Uses reflection; therefore, is slower than conversion enabled by IConvertible. |
Does not use reflection. |
Allows two-way type conversions from the custom type to other data types and from other data types to the custom type. For example, a TypeConverter defined for MyType allows conversions from MyType to String and from String to MyType. |
Allows conversion from a custom type to other data types but not from other data types to the custom type. |
Note A TypeConverter for a type is implemented outside the type and associated with the type by applying a TypeConverterAttribute attribute. |
Note IConvertible is implemented by a custom type. To convert a type, a user of the type invokes a conversion method (of the IConvertible contract) on the type. |
For details about using type converters to perform conversions, see System.ComponentModel.TypeConverter. For information about implementing a type converter to provide design-time support for a custom type, see Implementing a Type Converter.
See Also
Tasks
How to: Implement a Type Converter