Convert::ChangeType Method (Object, Type, 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, Type^ conversionType, IFormatProvider^ provider )
Parameters
- value
- Type: System::Object
An object that implements the IConvertible interface.
- conversionType
- Type: System::Type
The type of object to return.
- provider
- Type: System::IFormatProvider
An object that supplies culture-specific formatting information.
Return Value
Type: System::ObjectAn 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 nullptr and conversionType is not a value type.
| Exception | Condition |
|---|---|
| InvalidCastException | This conversion is not supported. -or- value is nullptr 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 nullptr. |
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.
The following example defines a Temperature class that implements the IConvertible interface.
using namespace System; using namespace System::Globalization; public ref class Temperature : IConvertible { private: Decimal m_Temp; public: Temperature(Decimal temperature) { m_Temp = temperature; } property Decimal Celsius { Decimal get() { return m_Temp; } } property Decimal Kelvin { Decimal get() { return m_Temp + (Decimal) 273.15; } } property Decimal Fahrenheit { Decimal get() { return Math::Round((Decimal) (m_Temp * 9 / 5 + 32), 2); } } virtual String^ ToString() override { return m_Temp.ToString("N2") + "�C"; } // IConvertible implementations. virtual TypeCode GetTypeCode() { return TypeCode::Object; } virtual bool ToBoolean(IFormatProvider^ provider) { if (m_Temp == 0) return false; else return true; } virtual Byte ToByte(IFormatProvider^ provider) { if (m_Temp < Byte::MinValue || m_Temp > Byte::MaxValue) throw gcnew OverflowException(String::Format("{0} is out of range of the Byte type.", m_Temp)); else return Decimal::ToByte(m_Temp); } virtual Char ToChar(IFormatProvider^ provider) { throw gcnew InvalidCastException("Temperature to Char conversion is not supported."); } virtual DateTime ToDateTime(IFormatProvider^ provider) { throw gcnew InvalidCastException("Temperature to DateTime conversion is not supported."); } virtual Decimal ToDecimal(IFormatProvider^ provider) { return m_Temp; } virtual Double ToDouble(IFormatProvider^ provider) { return Decimal::ToDouble(m_Temp); } virtual Int16 ToInt16(IFormatProvider^ provider) { if (m_Temp < Int16::MinValue || m_Temp > Int16::MaxValue) throw gcnew OverflowException(String::Format("{0} is out of range of the Int16 type.", m_Temp)); else return Decimal::ToInt16(m_Temp); } virtual Int32 ToInt32(IFormatProvider^ provider) { if (m_Temp < Int32::MinValue || m_Temp > Int32::MaxValue) throw gcnew OverflowException(String::Format("{0} is out of range of the Int32 type.", m_Temp)); else return Decimal::ToInt32(m_Temp); } virtual Int64 ToInt64(IFormatProvider^ provider) { if (m_Temp < Int64::MinValue || m_Temp > Int64::MaxValue) throw gcnew OverflowException(String::Format("{0} is out of range of the Int64 type.", m_Temp)); else return Decimal::ToInt64(m_Temp); } virtual SByte ToSByte(IFormatProvider^ provider) { if (m_Temp < SByte::MinValue || m_Temp > SByte::MaxValue) throw gcnew OverflowException(String::Format("{0} is out of range of the SByte type.", m_Temp)); else return Decimal::ToSByte(m_Temp); } virtual Single ToSingle(IFormatProvider^ provider) { return Decimal::ToSingle(m_Temp); } virtual String^ ToString(IFormatProvider^ provider) { return m_Temp.ToString("N2", provider) + "�C"; } virtual Object^ ToType(Type^ conversionType, IFormatProvider^ provider) { switch (Type::GetTypeCode(conversionType)) { case TypeCode::Boolean: return ToBoolean(nullptr); case TypeCode::Byte: return ToByte(nullptr); case TypeCode::Char: return ToChar(nullptr); case TypeCode::DateTime: return ToDateTime(nullptr); case TypeCode::Decimal: return ToDecimal(nullptr); case TypeCode::Double: return ToDouble(nullptr); case TypeCode::Int16: return ToInt16(nullptr); case TypeCode::Int32: return ToInt32(nullptr); case TypeCode::Int64: return ToInt64(nullptr); case TypeCode::Object: if (Temperature::typeid->Equals(conversionType)) return this; else throw gcnew InvalidCastException(String::Format("Conversion to a {0} is not supported.", conversionType->Name)); case TypeCode::SByte: return ToSByte(nullptr); case TypeCode::Single: return ToSingle(nullptr); case TypeCode::String: return ToString(provider); case TypeCode::UInt16: return ToUInt16(nullptr); case TypeCode::UInt32: return ToUInt32(nullptr); case TypeCode::UInt64: return ToUInt64(nullptr); default: throw gcnew InvalidCastException(String::Format("Conversion to {0} is not supported.", conversionType->Name)); } } virtual UInt16 ToUInt16(IFormatProvider^ provider) { if (m_Temp < UInt16::MinValue || m_Temp > UInt16::MaxValue) throw gcnew OverflowException(String::Format("{0} is out of range of the UInt16 type.", m_Temp)); else return Decimal::ToUInt16(m_Temp); } virtual UInt32 ToUInt32(IFormatProvider^ provider) { if (m_Temp < UInt32::MinValue || m_Temp > UInt32::MaxValue) throw gcnew OverflowException(String::Format("{0} is out of range of the UInt32 type.", m_Temp)); else return Decimal::ToUInt32(m_Temp); } virtual UInt64 ToUInt64(IFormatProvider^ provider) { if (m_Temp < UInt64::MinValue || m_Temp > UInt64::MaxValue) throw gcnew OverflowException(String::Format("{0} is out of range of the UInt64 type.", m_Temp)); else return Decimal::ToUInt64(m_Temp); } };
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.
void main() { Temperature^ cool = gcnew Temperature(5); array<Type^>^ targetTypes = gcnew array<Type^> { SByte::typeid, Int16::typeid, Int32::typeid, Int64::typeid, Byte::typeid, UInt16::typeid, UInt32::typeid, UInt64::typeid, Decimal::typeid, Single::typeid, Double::typeid, String::typeid }; CultureInfo^ provider = gcnew CultureInfo("fr-FR"); for each (Type^ targetType in targetTypes) { try { Object^ value = Convert::ChangeType(cool, targetType, provider); Console::WriteLine("Converted {0} {1} to {2} {3}.", cool->GetType()->Name, cool->ToString(), targetType->Name, value); } catch (InvalidCastException^) { Console::WriteLine("Unsupported {0} --> {1} conversion.", cool->GetType()->Name, targetType->Name); } catch (OverflowException^) { Console::WriteLine("{0} is out of range of the {1} type.", cool, targetType->Name); } } } // The example dosplays the following output: // Converted Temperature 5.00�C to SByte 5. // Converted Temperature 5.00�C to Int16 5. // Converted Temperature 5.00�C to Int32 5. // Converted Temperature 5.00�C to Int64 5. // Converted Temperature 5.00�C to Byte 5. // Converted Temperature 5.00�C to UInt16 5. // Converted Temperature 5.00�C to UInt32 5. // Converted Temperature 5.00�C to UInt64 5. // Converted Temperature 5.00�C to Decimal 5. // Converted Temperature 5.00�C to Single 5. // Converted Temperature 5.00�C to Double 5. // Converted Temperature 5.00�C to String 5,00�C.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.