Compartilhar via


Convert.ChangeType Method (Object, TypeCode, IFormatProvider)

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

Returns an object of the specified type whose value is equivalent to the specified object. A parameter supplies culture-specific formatting information.

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

Syntax

'Declaration
Public Shared Function ChangeType ( _
    value As Object, _
    typeCode As TypeCode, _
    provider As IFormatProvider _
) As Object
public static Object ChangeType(
    Object value,
    TypeCode typeCode,
    IFormatProvider provider
)

Parameters

Return Value

Type: System.Object
An object whose underlying type is typeCode and whose value is equivalent to value.
-or-
A null reference (Nothing in Visual Basic), if value is nulla null reference (Nothing in Visual Basic) and typeCode is Empty, String, or Object.

Exceptions

Exception Condition
InvalidCastException

This conversion is not supported.

-or-

value is nulla null reference (Nothing in Visual Basic), and typeCode specifies a value type.

-or-

value does not implement the IConvertible interface.

FormatException

value is not in a format for the typeCode type recognized by provider.

OverflowException

value represents a number that is out of the range of the typeCode type.

ArgumentException

typeCode is invalid.

Remarks

ChangeType(Object, TypeCode, IFormatProvider) is a general-purpose conversion method that converts the object specified by value to a predefined type specified by typeCode. The value parameter can be an object of any type. For the conversion to succeed, value must implement the IConvertible interface, because the method simply wraps a call to an appropriate IConvertible method. The method also requires that conversion of value to typeCode be supported.

The ChangeType(Object, TypeCode, IFormatProvider) method does not support the conversion of value to a custom type. To perform such a conversion, call the ChangeType(Object, Type, IFormatProvider) method.

The provider parameter is an IFormatProvider implementation that supplies formatting information for the conversion. If value is a base data type, provider is used only for the following conversions:

  • Conversion from a number to a string, or from a string to a number. provider must be a CultureInfo object, a NumberFormatInfo object, or a custom IFormatProvider implementation that returns a NumberFormatInfo object. However, because the ChangeType(Object, TypeCode, IFormatProvider) method performs the conversion using the default "G" format specifier, the provider parameter has no effect if value or the target type is an unsigned integer.

  • Conversion from a DateTime value to a string, or from a string to a DateTime value. provider must be a CultureInfo or DateTimeFormatInfo object.

If value is an application-defined type, its IConvertible implementation may use the provider parameter.

Examples

The following example defines a custom format provider named InterceptProvider that indicates when its GetFormat method is called and returns a NumberFormatInfo for the fr-FR culture and a DateTimeFormatInfo object for the en-US culture. This format provider is used in all calls to the ChangeType(Object, TypeCode, IFormatProvider) method. The example then creates an array with a Double and a DateTime value and makes repeated calls to ChangeType(Object, TypeCode, IFormatProvider) with each value and each member of the TypeCode enumeration. The example illustrates when the method uses the IFormatProvider parameter and also illustrates the use of the provider parameter to perform culture-sensitive formatting.

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.