TypeConverter Class

July 28, 2014

Provides a unified way of converting types of values to other types. TypeConverter typically supports string-to-object conversion, either for property editors in design environments, or to enable XAML usages.

System.Object
  System.ComponentModel.TypeConverter
    More...

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)

public class TypeConverter

The TypeConverter type exposes the following members.

  NameDescription
Public methodTypeConverterInitializes a new instance of the TypeConverter class.
Top

  NameDescription
Public methodCanConvertFrom(Type)Returns whether the type converter can convert an object from the specified type to the type of this converter.
Public methodCanConvertFrom(ITypeDescriptorContext, Type)Returns whether the type converter can convert an object from the specified type to the type of this converter.
Public methodCanConvertTo(Type)Returns whether the type converter can convert an object to the specified type.
Public methodCanConvertTo(ITypeDescriptorContext, Type)Returns whether the type converter can convert an object to the specified type.
Public methodConvertFrom(Object)Converts from the specified value to the type of this converter.
Public methodConvertFrom(ITypeDescriptorContext, CultureInfo, Object)Converts from the specified value to the intended conversion type of the converter.
Public methodConvertFromStringConverts the specified text to an object.
Public methodConvertTo(Object, Type)Converts the specified value object to the specified type.
Public methodConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)Converts the specified value object to the specified type.
Public methodConvertToStringConverts the specified object to a string representation.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The most common type of converter is one that converts to and from a text or string representation. The type converter for a class is associated with the class by applying a TypeConverterAttribute in the class definition. Unless this attribute is overridden, all classes that inherit from this class use the same type converter as the base class.

The most common scenario for TypeConverter in XAML is to support setting XAML attribute values and generating object values when XAML is parsed, both forXAML defined classes and custom classes.

Notes to Implementers

To support the string creation syntax of custom types that are specified as attributes in XAML, you should implement CanConvertFrom(ITypeDescriptorContext, Type) in your TypeConverter class.

For Windows Phone, implementing CanConvertTo and ConvertTo is not crucial, because XAML does not support a general serialization mechanism. However, you might still consider providing a basic CanConvertTo and ConvertTo implementation that reverses the ConvertFrom in your TypeConverter conversion. This will produce a more robust and generalized TypeConverter for anyone that specifically calls the ConvertTo method.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

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

Show:
© 2014 Microsoft