次の方法で共有


Convert.ChangeType Method (Object, Type, 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, _
    conversionType As Type, _
    provider As IFormatProvider _
) As Object
public static Object ChangeType(
    Object value,
    Type conversionType,
    IFormatProvider provider
)

Parameters

  • conversionType
    Type: System.Type
    The type of object to return.

Return Value

Type: System.Object
An object whose type is conversionType and whose value is equivalent to value.
-or-
value, if the type of value and conversionType are equal.
-or-
A null reference (Nothing in Visual Basic), if value is nulla null reference (Nothing in Visual Basic) and conversionType is not a value type.

Exceptions

Exception Condition
InvalidCastException

This conversion is not supported.

-or-

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

-or-

value does not implement the IConvertible interface.

FormatException

value is not in a format for conversionType recognized by provider.

OverflowException

value represents a number that is out of the range of conversionType.

ArgumentNullException

conversionType is nulla null reference (Nothing in Visual Basic).

Remarks

ChangeType is a general-purpose conversion method that converts the object specified by value to conversionType. The value parameter can be an object of any type, and conversionType can also be a Type object that represents any base or custom 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 requires that conversion of value to conversionType be supported.

provider enables the user to specify culture-specific conversion information about the contents of value. For example, if value is a String that represents a number, provider could supply culture-specific information about the notation used to represent that number.

Examples

The following example defines a Temperature class that implements the IConvertible interface.

The following example creates an instance of the Temperature class and calls the ChangeType(Object, Type, IFormatProvider) method to convert it to the basic numeric types supported by the .NET Framework and to a String. It illustrates that the ChangeType method wraps a call to the source type's IConvertible implementation.

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.