public static object ChangeType (object value, TypeSpec targetType, out bool error)
{
IConvertible convert_value = value as IConvertible;
if (convert_value == null){
error = true;
return null;
}
//
// We cannot rely on build-in type conversions as they are
// more limited than what C# supports.
// See char -> float/decimal/double conversion
//
error = false;
try {
if (targetType == TypeManager.bool_type)
return convert_value.ToBoolean (nf_provider);
if (targetType == TypeManager.byte_type)
return convert_value.ToByte (nf_provider);
if (targetType == TypeManager.char_type)
return convert_value.ToChar (nf_provider);
if (targetType == TypeManager.short_type)
return convert_value.ToInt16 (nf_provider);
if (targetType == TypeManager.int32_type)
return convert_value.ToInt32 (nf_provider);
if (targetType == TypeManager.int64_type)
return convert_value.ToInt64 (nf_provider);
if (targetType == TypeManager.sbyte_type)
return convert_value.ToSByte (nf_provider);
if (targetType == TypeManager.decimal_type) {
if (convert_value.GetType () == typeof (char))
return (decimal) convert_value.ToInt32 (nf_provider);
return convert_value.ToDecimal (nf_provider);
}
if (targetType == TypeManager.double_type) {
if (convert_value.GetType () == typeof (char))
return (double) convert_value.ToInt32 (nf_provider);
return convert_value.ToDouble (nf_provider);
}
if (targetType == TypeManager.float_type) {
if (convert_value.GetType () == typeof (char))
return (float)convert_value.ToInt32 (nf_provider);
return convert_value.ToSingle (nf_provider);
}
if (targetType == TypeManager.string_type)
return convert_value.ToString (nf_provider);
if (targetType == TypeManager.ushort_type)
return convert_value.ToUInt16 (nf_provider);
if (targetType == TypeManager.uint32_type)
return convert_value.ToUInt32 (nf_provider);
if (targetType == TypeManager.uint64_type)
return convert_value.ToUInt64 (nf_provider);
if (targetType == TypeManager.object_type)
return value;
error = true;
} catch {
error = true;
}
return null;
}