diff options
Diffstat (limited to 'mcs/class/corlib/System/Convert.cs')
-rw-r--r-- | mcs/class/corlib/System/Convert.cs | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/mcs/class/corlib/System/Convert.cs b/mcs/class/corlib/System/Convert.cs index f63481831c5..5e15d578704 100644 --- a/mcs/class/corlib/System/Convert.cs +++ b/mcs/class/corlib/System/Convert.cs @@ -2559,8 +2559,18 @@ namespace System { return (long) result;
}
+ private static void EndianSwap (ref byte[] value) + { + byte[] buf = new byte[value.Length]; + for (int i = 0; i < value.Length; i++) + buf[i] = value[value.Length-1-i]; + value = buf; + } + private static string ConvertToBase2 (byte[] value) { + if (!BitConverter.IsLittleEndian) + EndianSwap (ref value); StringBuilder sb = new StringBuilder (); for (int i = value.Length - 1; i >= 0; i--) { byte b = value [i]; @@ -2580,6 +2590,8 @@ namespace System { private static string ConvertToBase8 (byte[] value)
{ + if (!BitConverter.IsLittleEndian) + EndianSwap (ref value); ulong l = 0; switch (value.Length) { case 1: @@ -2612,6 +2624,8 @@ namespace System { private static string ConvertToBase16 (byte[] value) { + if (!BitConverter.IsLittleEndian) + EndianSwap (ref value); StringBuilder sb = new StringBuilder (); for (int i = value.Length - 1; i >= 0; i--) { char high = (char)((value[i] >> 4) & 0x0f); @@ -2672,8 +2686,12 @@ namespace System { internal static object ToType (object value, Type conversionType,
IFormatProvider provider)
{
- if (value == null)
- return null;
+ if (value == null) { + if ((conversionType != null) && conversionType.IsValueType) + throw new InvalidCastException ("Null object can not be converted to a value type."); + else + return null; + } if (conversionType == null)
throw new InvalidCastException ("Cannot cast to destination type.");
@@ -2739,12 +2757,7 @@ namespace System { else if (conversionType == conversionTable[18]) // 18 TypeCode.String
return (object) convertValue.ToString (provider);
else {
- try {
- return (object) convertValue;
- }
- catch {
- throw new ArgumentException (Locale.GetText ("Unknown target conversion type"));
- }
+ throw new ArgumentException (Locale.GetText ("Unknown target conversion type"));
}
} else
// Not in the conversion table
|