Convert.ChangeType Method (Object, TypeCode, IFormatProvider)
Returns an object of the specified type whose value is equivalent to the specified object. A parameter supplies culture-specific formatting information.
Assembly: mscorlib (in mscorlib.dll)
public static Object ChangeType( Object value, TypeCode typeCode, IFormatProvider provider )
- Type: System.TypeCode
The type of object to return.
- Type: System.IFormatProvider
An object that supplies culture-specific formatting information.
This conversion is not supported.
value is null, and typeCode specifies a value type.
value does not implement the IConvertible interface.
value is not in a format for the typeCode type recognized by provider.
value represents a number that is out of the range of the typeCode type.
typeCode is invalid.
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 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 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.
If value is an application-defined type, its IConvertible implementation may use the provider parameter.
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 method. The example then creates an array with a Double and a DateTime value and makes repeated calls to 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.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.