共用方式為


Generalized Type Conversion

The .NET Framework provides the following two mechanisms for converting user-defined data types (custom types) to other data types:

The following table highlights the differences between these two mechanisms.

NoteNote

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.

NoteNote

A TypeConverter for a type is implemented outside the type and associated with the type by applying a TypeConverterAttribute attribute.

NoteNote

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

Other Resources

Extending Design-Time Support