共用方式為


TypeConverter Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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.

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

Syntax

'Declaration
Public Class TypeConverter
public class TypeConverter

The TypeConverter type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 TypeConverter Initializes a new instance of the TypeConverter class.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 CanConvertFrom(Type) Returns whether the type converter can convert an object from the specified type to the type of this converter.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 CanConvertFrom(ITypeDescriptorContext, Type) Returns whether the type converter can convert an object from the specified type to the type of this converter.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 CanConvertTo(Type) Returns whether the type converter can convert an object to the specified type.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 CanConvertTo(ITypeDescriptorContext, Type) Returns whether the type converter can convert an object to the specified type.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ConvertFrom(Object) Converts from the specified value to the type of this converter.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ConvertFrom(ITypeDescriptorContext, CultureInfo, Object) Converts from the specified value to the intended conversion type of the converter.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ConvertFromString Converts the specified text to an object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ConvertTo(Object, Type) Converts the specified value object to the specified type.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) Converts the specified value object to the specified type.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ConvertToString Converts the specified object to a string representation.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

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 Silverlight is to support setting XAML attribute values and generating object values when XAML is parsed, both for Silverlight defined classes and custom classes. See TypeConverters and XAML.

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) and ConvertFrom(ITypeDescriptorContext, CultureInfo, Object) in your TypeConverter class.

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

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.

See Also

Reference

Other Resources