**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System / Convert.cs
blob76a57c491d47232cc4cb8eab99b492286066b2b4
1 //
2 // System.Convert.cs
3 //
4 // Author:
5 // Derek Holden (dholden@draper.com)
6 // Duncan Mak (duncan@ximian.com)
7 //
8 // (C) Ximian, Inc. http://www.ximian.com
9 //
11 // System.Convert class. This was written word for word off the
12 // Library specification for System.Convert in the ECMA TC39 TG2
13 // and TG3 working documents. The first page of which has a table
14 // for all legal conversion scenerios.
16 // This header and the one above it can be formatted however, just trying
17 // to keep it consistent w/ the existing mcs headers.
19 // This Convert class could be written another way, with each type
20 // implementing IConvertible and defining their own conversion functions,
21 // and this class just calling the type's implementation. Or, they can
22 // be defined here and the implementing type can use these functions when
23 // defining their IConvertible interface. Byte's ToBoolean() calls
24 // Convert.ToBoolean(byte), or Convert.ToBoolean(byte) calls
25 // byte.ToBoolean(). The first case is what is done here.
27 // See http://lists.ximian.com/archives/public/mono-list/2001-July/000525.html
29 // There are also conversion functions that are not defined in
30 // the ECMA draft, such as there is no bool ToBoolean(DateTime value),
31 // and placing that somewhere won't compile w/ this Convert since the
32 // function doesn't exist. However calling that when using Microsoft's
33 // System.Convert doesn't produce any compiler errors, it just throws
34 // an InvalidCastException at runtime.
36 // Whenever a decimal, double, or single is converted to an integer
37 // based type, it is even rounded. This uses Math.Round which only
38 // has Round(decimal) and Round(double), so in the Convert from
39 // single cases the value is passed to Math as a double. This
40 // may not be completely necessary.
42 // The .NET Framework SDK lists DBNull as a member of this class
43 // as 'public static readonly object DBNull;'.
45 // It should also be decided if all the cast return values should be
46 // returned as unchecked or not.
48 // All the XML function comments were auto generated which is why they
49 // sound someone redundant.
51 // TYPE | BOOL BYTE CHAR DT DEC DBL I16 I32 I64 SBYT SNGL STR UI16 UI32 UI64
52 // -----+--------------------------------------------------------------------
53 // BOOL | X X X X X X X X X X X X X
54 // BYTE | X X X X X X X X X X X X X X
55 // CHAR | X X X X X X X X X X
56 // DT | X X
57 // DEC | X X X X X X X X X X X X X
58 // DBL | X X X X X X X X X X X X X
59 // I16 | X X X X X X X X X X X X X X
60 // I32 | X X X X X X X X X X X X X X
61 // I64 | X X X X X X X X X X X X X X
62 // SBYT | X X X X X X X X X X X X X X
63 // SNGL | X X X X X X X X X X X X X
64 // STR | X X X X X X X X X X X X X X X
65 // UI16 | X X X X X X X X X X X X X X
66 // UI32 | X X X X X X X X X X X X X X
67 // UI64 | X X X X X X X X X X X X X X
71 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
73 // Permission is hereby granted, free of charge, to any person obtaining
74 // a copy of this software and associated documentation files (the
75 // "Software"), to deal in the Software without restriction, including
76 // without limitation the rights to use, copy, modify, merge, publish,
77 // distribute, sublicense, and/or sell copies of the Software, and to
78 // permit persons to whom the Software is furnished to do so, subject to
79 // the following conditions:
80 //
81 // The above copyright notice and this permission notice shall be
82 // included in all copies or substantial portions of the Software.
83 //
84 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
85 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
86 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
87 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
88 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
89 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
90 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
93 using System.Globalization;
94 using System.IO;
95 using System.Security.Cryptography;
96 using System.Text;
98 namespace System {
100 // [CLSCompliant(false)]
101 public sealed class Convert {
103 // Fields
104 public static readonly object DBNull = System.DBNull.Value;
105 static ToBase64Transform toBase64Transform = new ToBase64Transform();
107 private Convert ()
111 // ========== BASE 64 Conversions ========== //
112 // the BASE64 convert methods are using the Base64 converting methods
113 // from System.Security.Cryptography.ToBase64Transform and
114 // System.Security.Cryptography.FromBase64Transform
116 // should be changed to a stand-alone class Base64Encoder & Base64Decoder
118 public static byte[] FromBase64CharArray (char[] inArray, int offset, int length)
120 if (inArray == null)
121 throw new ArgumentNullException ("inArray");
122 if (offset < 0)
123 throw new ArgumentOutOfRangeException ("offset < 0");
124 if (length < 0)
125 throw new ArgumentOutOfRangeException ("length < 0");
126 // avoid integer overflow
127 if (offset > inArray.Length - length)
128 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
129 // do not check length here (multiple of 4) because the
130 // string can contain ignored characters
132 return FromBase64 (inArray, offset, length);
135 public static byte[] FromBase64String (string s)
137 if (s == null)
138 throw new ArgumentNullException ("s");
139 // do not check length here (multiple of 4) because the
140 // string can contain ignored characters
142 char[] chars = s.ToCharArray ();
143 return FromBase64 (chars, 0, chars.Length);
146 private static byte[] FromBase64 (char[] chars, int index, int count)
148 byte[] data = new byte [count];
149 int n = 0;
150 for (int i=0; i < count; i++) {
151 char c = chars [i];
152 // drop ignored characters
153 if (c == '\t' || c == '\r' || c == '\n' || c == ' ')
154 continue;
155 // wide chars (16 bits)
156 if ((int)c > Byte.MaxValue)
157 continue;
158 data [n++] = (byte) c;
160 // now the length must be a multiple of 4 bytes (same as % 4)
161 if ((n & 3) != 0)
162 throw new FormatException ("invalid base64 length");
163 // and we do not need to ignore whitespace this time (which is a little faster)
164 FromBase64Transform t = new FromBase64Transform (FromBase64TransformMode.DoNotIgnoreWhiteSpaces);
165 return t.TransformFinalBlock (data, 0, n);
168 public static TypeCode GetTypeCode (object value)
170 if (value == null)
171 return TypeCode.Empty;
172 else
173 return Type.GetTypeCode (value.GetType ());
176 public static bool IsDBNull (object value)
178 if (value is DBNull)
179 return true;
180 else
181 return false;
184 public static int ToBase64CharArray (byte[] inArray, int offsetIn, int length,
185 char[] outArray, int offsetOut)
187 if (inArray == null)
188 throw new ArgumentNullException ("inArray");
189 if (outArray == null)
190 throw new ArgumentNullException ("outArray");
191 if (offsetIn < 0 || length < 0 || offsetOut < 0)
192 throw new ArgumentOutOfRangeException ("offsetIn, length, offsetOut < 0");
193 // avoid integer overflow
194 if (offsetIn > inArray.Length - length)
195 throw new ArgumentOutOfRangeException ("offsetIn + length > array.Length");
197 // note: normally ToBase64Transform doesn't support multiple block processing
198 byte[] outArr = toBase64Transform.InternalTransformFinalBlock (inArray, offsetIn, length);
200 char[] cOutArr = new ASCIIEncoding ().GetChars (outArr);
202 // avoid integer overflow
203 if (offsetOut > outArray.Length - cOutArr.Length)
204 throw new ArgumentOutOfRangeException ("offsetOut + cOutArr.Length > outArray.Length");
206 Array.Copy (cOutArr, 0, outArray, offsetOut, cOutArr.Length);
208 return cOutArr.Length;
211 public static string ToBase64String (byte[] inArray)
213 if (inArray == null)
214 throw new ArgumentNullException ("inArray");
216 return ToBase64String (inArray, 0, inArray.Length);
219 public static string ToBase64String (byte[] inArray, int offset, int length)
221 if (inArray == null)
222 throw new ArgumentNullException ("inArray");
223 if (offset < 0 || length < 0)
224 throw new ArgumentOutOfRangeException ("offset < 0 || length < 0");
225 // avoid integer overflow
226 if (offset > inArray.Length - length)
227 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
229 // note: normally ToBase64Transform doesn't support multiple block processing
230 byte[] outArr = toBase64Transform.InternalTransformFinalBlock (inArray, offset, length);
232 return (new ASCIIEncoding ().GetString (outArr));
235 #if NET_2_0
236 public static string ToBase64String (byte[] inArray, Base64FormattingOptions options)
238 if (inArray == null)
239 throw new ArgumentNullException ("inArray");
240 return ToBase64String (inArray, 0, inArray.Length, options);
243 public static string ToBase64String (byte[] inArray, bool insertLineBreaks)
245 Base64FormattingOptions options = insertLineBreaks ? Base64FormattingOptions.InsertLineBreaks : Base64FormattingOptions.None;
246 return ToBase64String (inArray, options);
249 public static string ToBase64String (byte[] inArray, int offset, int length, Base64FormattingOptions options)
251 if (inArray == null)
252 throw new ArgumentNullException ("inArray");
253 if (offset < 0 || length < 0)
254 throw new ArgumentOutOfRangeException ("offset < 0 || length < 0");
255 // avoid integer overflow
256 if (offset > inArray.Length - length)
257 throw new ArgumentOutOfRangeException ("offset + length > array.Length");
259 Encoding encoding = new ASCIIEncoding ();
260 StringBuilder sb = new StringBuilder ();
261 BinaryReader reader = new BinaryReader (new MemoryStream (inArray, offset, length));
262 byte[] b = null;
264 do {
265 // 54 bytes of input makes for 72 bytes of output.
266 b = reader.ReadBytes (54);
267 if (b.Length > 0)
268 sb.AppendLine (encoding.GetString (toBase64Transform.InternalTransformFinalBlock (b, 0, b.Length)));
269 } while (b.Length > 0);
271 return sb.ToString ();
274 public static string ToBase64String (byte[] inArray, int offset, int length, bool insertLineBreaks)
276 Base64FormattingOptions options = insertLineBreaks ? Base64FormattingOptions.InsertLineBreaks : Base64FormattingOptions.None;
277 return ToBase64String (inArray, offset, length, options);
279 #endif
283 // ========== Boolean Conversions ========== //
285 public static bool ToBoolean (bool value)
287 return value;
290 public static bool ToBoolean (byte value)
292 return (value != 0);
295 public static bool ToBoolean (char value)
297 throw new InvalidCastException (Locale.GetText ("Can't convert char to bool"));
300 public static bool ToBoolean (DateTime value)
302 throw new InvalidCastException (Locale.GetText ("Can't convert date to bool"));
305 public static bool ToBoolean (decimal value)
307 return (value != 0M);
310 public static bool ToBoolean (double value)
312 return (value != 0);
315 public static bool ToBoolean (float value)
317 return (value != 0f);
320 public static bool ToBoolean (int value)
322 return (value != 0);
325 public static bool ToBoolean (long value)
327 return (value != 0);
330 [CLSCompliant (false)]
331 public static bool ToBoolean (sbyte value)
333 return (value != 0);
336 public static bool ToBoolean (short value)
338 return (value != 0);
341 public static bool ToBoolean (string value)
343 if (value == null)
344 return false; // LAMESPEC: Spec says throw ArgumentNullException
345 return Boolean.Parse (value);
348 public static bool ToBoolean (string value, IFormatProvider provider)
350 if (value == null)
351 return false; // LAMESPEC: Spec says throw ArgumentNullException
352 return Boolean.Parse (value); // provider is ignored.
355 [CLSCompliant (false)]
356 public static bool ToBoolean (uint value)
358 return (value != 0);
361 [CLSCompliant (false)]
362 public static bool ToBoolean (ulong value)
364 return (value != 0);
367 [CLSCompliant (false)]
368 public static bool ToBoolean (ushort value)
370 //if (value == null)
371 // return false;
372 return (value != 0);
375 public static bool ToBoolean (object value)
377 if (value == null)
378 return false;
379 return ToBoolean (value, null);
382 public static bool ToBoolean (object value, IFormatProvider provider)
384 if (value == null)
385 return false;
386 return ((IConvertible) value).ToBoolean (provider);
389 // ========== Byte Conversions ========== //
391 public static byte ToByte (bool value)
393 return (byte)(value ? 1 : 0);
396 public static byte ToByte (byte value)
398 return value;
401 public static byte ToByte (char value)
403 if (value > Byte.MaxValue)
404 throw new OverflowException (Locale.GetText (
405 "Value is greater than Byte.MaxValue"));
407 return (byte)value;
410 public static byte ToByte (DateTime value)
412 throw new InvalidCastException ("This conversion is not supported.");
415 public static byte ToByte (decimal value)
417 if (value > Byte.MaxValue || value < Byte.MinValue)
418 throw new OverflowException (Locale.GetText (
419 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
421 // Returned Even-Rounded
422 return (byte)(Math.Round (value));
425 public static byte ToByte (double value)
427 if (value > Byte.MaxValue || value < Byte.MinValue)
428 throw new OverflowException (Locale.GetText (
429 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
431 // This and the float version of ToByte are the only ones
432 // the spec listed as checking for .NaN and Infinity overflow
433 if (Double.IsNaN(value) || Double.IsInfinity(value))
434 throw new OverflowException (Locale.GetText (
435 "Value is equal to Double.NaN, Double.PositiveInfinity, or Double.NegativeInfinity"));
437 // Returned Even-Rounded
438 return (byte)(Math.Round (value));
441 public static byte ToByte (float value)
443 if (value > Byte.MaxValue || value < Byte.MinValue)
444 throw new OverflowException (Locale.GetText (
445 "Value is greater than Byte.MaxValue or less than Byte.Minalue"));
447 // This and the double version of ToByte are the only ones
448 // the spec listed as checking for .NaN and Infinity overflow
449 if (Single.IsNaN(value) || Single.IsInfinity(value))
450 throw new OverflowException (Locale.GetText (
451 "Value is equal to Single.NaN, Single.PositiveInfinity, or Single.NegativeInfinity"));
453 // Returned Even-Rounded, pass it as a double, could have this
454 // method just call Convert.ToByte ( (double)value)
455 return (byte)(Math.Round ( (double)value));
458 public static byte ToByte (int value)
460 if (value > Byte.MaxValue || value < Byte.MinValue)
461 throw new OverflowException (Locale.GetText (
462 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
464 return (byte)value;
467 public static byte ToByte (long value)
469 if (value > Byte.MaxValue || value < Byte.MinValue)
470 throw new OverflowException (Locale.GetText (
471 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
473 return (byte)value;
476 [CLSCompliant (false)]
477 public static byte ToByte (sbyte value)
479 if (value < Byte.MinValue)
480 throw new OverflowException (Locale.GetText (
481 "Value is less than Byte.MinValue"));
483 return (byte)value;
486 public static byte ToByte (short value)
488 if (value > Byte.MaxValue || value < Byte.MinValue)
489 throw new OverflowException (Locale.GetText (
490 "Value is greater than Byte.MaxValue or less than Byte.MinValue"));
492 return (byte)value;
495 public static byte ToByte (string value)
497 if (value == null)
498 return 0; // LAMESPEC: Spec says throw ArgumentNullException
499 return Byte.Parse (value);
502 public static byte ToByte (string value, IFormatProvider provider)
504 if (value == null)
505 return 0; // LAMESPEC: Spec says throw ArgumentNullException
506 return Byte.Parse (value, provider);
509 public static byte ToByte (string value, int fromBase)
511 int retVal = ConvertFromBase (value, fromBase, true);
513 if (retVal < (int) Byte.MinValue || retVal > (int) Byte.MaxValue)
514 throw new OverflowException ();
515 else
516 return (byte) retVal;
519 [CLSCompliant (false)]
520 public static byte ToByte (uint value)
522 if (value > Byte.MaxValue)
523 throw new OverflowException (Locale.GetText (
524 "Value is greater than Byte.MaxValue"));
526 return (byte)value;
529 [CLSCompliant (false)]
530 public static byte ToByte (ulong value)
532 if (value > Byte.MaxValue)
533 throw new OverflowException (Locale.GetText (
534 "Value is greater than Byte.MaxValue"));
536 return (byte)value;
539 [CLSCompliant (false)]
540 public static byte ToByte (ushort value)
542 if (value > Byte.MaxValue)
543 throw new OverflowException (Locale.GetText (
544 "Value is greater than Byte.MaxValue"));
546 return (byte)value;
549 public static byte ToByte (object value)
551 if (value == null)
552 return 0;
553 return ToByte (value, null);
556 public static byte ToByte (object value, IFormatProvider provider)
558 if (value == null)
559 return 0;
560 return ((IConvertible) value).ToByte (provider);
563 // ========== Char Conversions ========== //
565 public static char ToChar (bool value)
567 throw new InvalidCastException ("This conversion is not supported.");
570 public static char ToChar (byte value)
572 return (char)value;
575 public static char ToChar (char value)
577 return value;
580 public static char ToChar (DateTime value)
582 throw new InvalidCastException ("This conversion is not supported.");
585 public static char ToChar (decimal value)
587 throw new InvalidCastException ("This conversion is not supported.");
590 public static char ToChar (double value)
592 throw new InvalidCastException ("This conversion is not supported.");
595 public static char ToChar (int value)
597 if (value > Char.MaxValue || value < Char.MinValue)
598 throw new OverflowException (Locale.GetText (
599 "Value is greater than Char.MaxValue or less than Char.MinValue"));
601 return (char)value;
604 public static char ToChar (long value)
606 if (value > Char.MaxValue || value < Char.MinValue)
607 throw new OverflowException (Locale.GetText (
608 "Value is greater than Char.MaxValue or less than Char.MinValue"));
610 return (char)value;
613 public static char ToChar (float value)
615 throw new InvalidCastException ("This conversion is not supported.");
618 [CLSCompliant (false)]
619 public static char ToChar (sbyte value)
621 if (value < Char.MinValue)
622 throw new OverflowException (Locale.GetText (
623 "Value is less than Char.MinValue"));
625 return (char)value;
628 public static char ToChar (short value)
630 if (value < Char.MinValue)
631 throw new OverflowException (Locale.GetText (
632 "Value is less than Char.MinValue"));
634 return (char)value;
637 public static char ToChar (string value)
639 return Char.Parse (value);
642 public static char ToChar (string value, IFormatProvider provider)
644 return Char.Parse (value); // provider is ignored.
647 [CLSCompliant (false)]
648 public static char ToChar (uint value)
650 if (value > Char.MaxValue)
651 throw new OverflowException (Locale.GetText (
652 "Value is greater than Char.MaxValue"));
654 return (char)value;
657 [CLSCompliant (false)]
658 public static char ToChar (ulong value)
660 if (value > Char.MaxValue)
661 throw new OverflowException (Locale.GetText (
662 "Value is greater than Char.MaxValue"));
664 return (char)value;
667 [CLSCompliant (false)]
668 public static char ToChar (ushort value)
670 if (value > Char.MaxValue)
671 throw new OverflowException (Locale.GetText (
672 "Value is greater than Char.MaxValue"));
674 return (char)value;
677 public static char ToChar (object value)
679 if (value == null)
680 return '\0';
681 return ToChar (value, null);
684 public static char ToChar (object value, IFormatProvider provider)
686 if (value == null)
687 return '\0';
688 return ((IConvertible) value).ToChar (provider);
691 // ========== DateTime Conversions ========== //
693 public static DateTime ToDateTime (string value)
695 if (value == null)
696 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
697 return DateTime.Parse (value);
700 public static DateTime ToDateTime (string value, IFormatProvider provider)
702 if (value == null)
703 return DateTime.MinValue; // LAMESPEC: Spec says throw ArgumentNullException
704 return DateTime.Parse (value, provider);
707 public static DateTime ToDateTime (bool value)
709 throw new InvalidCastException ("This conversion is not supported.");
712 public static DateTime ToDateTime (byte value)
714 throw new InvalidCastException ("This conversion is not supported.");
717 public static DateTime ToDateTime (char value)
719 throw new InvalidCastException ("This conversion is not supported.");
722 public static DateTime ToDateTime (DateTime value)
724 return value;
727 public static DateTime ToDateTime (decimal value)
729 throw new InvalidCastException ("This conversion is not supported.");
732 public static DateTime ToDateTime (double value)
734 throw new InvalidCastException ("This conversion is not supported.");
737 public static DateTime ToDateTime (short value)
739 throw new InvalidCastException ("This conversion is not supported.");
742 public static DateTime ToDateTime (int value)
744 throw new InvalidCastException ("This conversion is not supported.");
747 public static DateTime ToDateTime (long value)
749 throw new InvalidCastException ("This conversion is not supported.");
752 public static DateTime ToDateTime (float value)
754 throw new InvalidCastException ("This conversion is not supported.");
757 public static DateTime ToDateTime (object value)
759 if (value == null)
760 return DateTime.MinValue;
761 return ToDateTime (value, null);
764 public static DateTime ToDateTime (object value, IFormatProvider provider)
766 if (value == null)
767 return DateTime.MinValue;
768 return ((IConvertible) value).ToDateTime (provider);
771 [CLSCompliant (false)]
772 public static DateTime ToDateTime (sbyte value)
774 throw new InvalidCastException ("This conversion is not supported.");
776 [CLSCompliant (false)]
777 public static DateTime ToDateTime (ushort value)
779 throw new InvalidCastException ("This conversion is not supported.");
782 [CLSCompliant (false)]
783 public static DateTime ToDateTime (uint value)
785 throw new InvalidCastException ("This conversion is not supported.");
788 [CLSCompliant (false)]
789 public static DateTime ToDateTime (ulong value)
791 throw new InvalidCastException ("This conversion is not supported.");
794 // ========== Decimal Conversions ========== //
796 public static decimal ToDecimal (bool value)
798 return value ? 1 : 0;
801 public static decimal ToDecimal (byte value)
803 return (decimal)value;
806 public static decimal ToDecimal (char value)
808 throw new InvalidCastException ("This conversion is not supported.");
811 public static decimal ToDecimal (DateTime value)
813 throw new InvalidCastException ("This conversion is not supported.");
816 public static decimal ToDecimal (decimal value)
818 return value;
821 public static decimal ToDecimal (double value)
823 if (value > (double)Decimal.MaxValue || value < (double)Decimal.MinValue)
824 throw new OverflowException (Locale.GetText (
825 "Value is greater than Decimal.MaxValue or less than Decimal.MinValue"));
827 return (decimal)value;
830 public static decimal ToDecimal (float value)
832 return (decimal) value;
835 public static decimal ToDecimal (int value)
837 return (decimal)value;
840 public static decimal ToDecimal (long value)
842 return (decimal)value;
845 [CLSCompliant (false)]
846 public static decimal ToDecimal (sbyte value)
848 return (decimal)value;
851 public static decimal ToDecimal (short value)
853 return (decimal)value;
856 public static decimal ToDecimal (string value)
858 if (value == null)
859 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
860 return Decimal.Parse (value);
863 public static decimal ToDecimal (string value, IFormatProvider provider)
865 if (value == null)
866 return new Decimal (0); // LAMESPEC: Spec says throw ArgumentNullException
867 return Decimal.Parse (value, provider);
870 [CLSCompliant (false)]
871 public static decimal ToDecimal (uint value)
873 return (decimal)value;
876 [CLSCompliant (false)]
877 public static decimal ToDecimal (ulong value)
879 return (decimal)value;
882 [CLSCompliant (false)]
883 public static decimal ToDecimal (ushort value)
885 return (decimal)value;
888 public static decimal ToDecimal (object value)
890 if (value == null)
891 return new Decimal (0);
892 return ToDecimal (value, null);
895 public static decimal ToDecimal (object value, IFormatProvider provider)
897 if (value == null)
898 return new Decimal (0);
899 return ((IConvertible) value).ToDecimal (provider);
903 // ========== Double Conversions ========== //
905 public static double ToDouble (bool value)
907 return value ? 1 : 0;
910 public static double ToDouble (byte value)
912 return (double) value;
915 public static double ToDouble (char value)
917 throw new InvalidCastException ("This conversion is not supported.");
920 public static double ToDouble (DateTime value)
922 throw new InvalidCastException ("This conversion is not supported.");
925 public static double ToDouble (decimal value)
927 return (double)value;
930 public static double ToDouble (double value)
932 return value;
935 public static double ToDouble (float value)
937 return (double) value;
940 public static double ToDouble (int value)
942 return (double)value;
945 public static double ToDouble (long value)
947 return (double)value;
950 [CLSCompliant (false)]
951 public static double ToDouble (sbyte value)
953 return (double)value;
956 public static double ToDouble (short value)
958 return (double)value;
961 public static double ToDouble (string value)
963 if (value == null)
964 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
965 return Double.Parse (value);
968 public static double ToDouble (string value, IFormatProvider provider)
970 if (value == null)
971 return 0.0; // LAMESPEC: Spec says throw ArgumentNullException
972 return Double.Parse (value, provider);
975 [CLSCompliant (false)]
976 public static double ToDouble (uint value)
978 return (double)value;
981 [CLSCompliant (false)]
982 public static double ToDouble (ulong value)
984 return (double)value;
987 [CLSCompliant (false)]
988 public static double ToDouble (ushort value)
990 return (double)value;
993 public static double ToDouble (object value)
995 if (value == null)
996 return 0.0;
997 return ToDouble (value, null);
1000 public static double ToDouble (object value, IFormatProvider provider)
1002 if (value == null)
1003 return 0.0;
1004 return ((IConvertible) value).ToDouble (provider);
1007 // ========== Int16 Conversions ========== //
1009 public static short ToInt16 (bool value)
1011 return (short)(value ? 1 : 0);
1014 public static short ToInt16 (byte value)
1016 return (short)value;
1019 public static short ToInt16 (char value)
1021 if (value > Int16.MaxValue)
1022 throw new OverflowException (Locale.GetText (
1023 "Value is greater than Int16.MaxValue"));
1025 return (short)value;
1028 public static short ToInt16 (DateTime value)
1030 throw new InvalidCastException ("This conversion is not supported.");
1033 public static short ToInt16 (decimal value)
1035 if (value > Int16.MaxValue || value < Int16.MinValue)
1036 throw new OverflowException (Locale.GetText (
1037 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1039 // Returned Even-Rounded
1040 return (short)(Math.Round (value));
1043 public static short ToInt16 (double value)
1045 if (value > Int16.MaxValue || value < Int16.MinValue)
1046 throw new OverflowException (Locale.GetText (
1047 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1049 // Returned Even-Rounded
1050 return (short)(Math.Round (value));
1053 public static short ToInt16 (float value)
1055 if (value > Int16.MaxValue || value < Int16.MinValue)
1056 throw new OverflowException (Locale.GetText (
1057 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1059 // Returned Even-Rounded, use Math.Round pass as a double.
1060 return (short)Math.Round ( (double)value);
1063 public static short ToInt16 (int value)
1065 if (value > Int16.MaxValue || value < Int16.MinValue)
1066 throw new OverflowException (Locale.GetText (
1067 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1069 return (short)value;
1072 public static short ToInt16 (long value)
1074 if (value > Int16.MaxValue || value < Int16.MinValue)
1075 throw new OverflowException (Locale.GetText (
1076 "Value is greater than Int16.MaxValue or less than Int16.MinValue"));
1078 return (short)value;
1081 [CLSCompliant (false)]
1082 public static short ToInt16 (sbyte value)
1084 return (short)value;
1087 public static short ToInt16 (short value)
1089 return value;
1092 public static short ToInt16 (string value)
1094 if (value == null)
1095 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1096 return Int16.Parse (value);
1099 public static short ToInt16 (string value, IFormatProvider provider)
1101 if (value == null)
1102 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1103 return Int16.Parse (value, provider);
1106 public static short ToInt16 (string value, int fromBase)
1108 int result = ConvertFromBase (value, fromBase, false);
1109 if (fromBase != 10) {
1110 // note: no sign are available to detect negatives
1111 if (result > Int16.MaxValue) {
1112 // return negative 2's complement
1113 return Convert.ToInt16 (-(65536 - result));
1116 return Convert.ToInt16 (result);
1119 [CLSCompliant (false)]
1120 public static short ToInt16 (uint value)
1122 if (value > Int16.MaxValue)
1123 throw new OverflowException (Locale.GetText (
1124 "Value is greater than Int16.MaxValue"));
1126 return (short)value;
1129 [CLSCompliant (false)]
1130 public static short ToInt16 (ulong value)
1132 if (value > (ulong)Int16.MaxValue)
1133 throw new OverflowException (Locale.GetText (
1134 "Value is greater than Int16.MaxValue"));
1135 return (short)value;
1138 [CLSCompliant (false)]
1139 public static short ToInt16 (ushort value)
1141 if (value > Int16.MaxValue)
1142 throw new OverflowException (Locale.GetText (
1143 "Value is greater than Int16.MaxValue"));
1145 return (short)value;
1148 public static short ToInt16 (object value)
1150 if (value == null)
1151 return 0;
1152 return ToInt16 (value, null);
1155 public static short ToInt16 (object value, IFormatProvider provider)
1157 if (value == null)
1158 return 0;
1159 return ((IConvertible) value).ToInt16 (provider);
1162 // ========== Int32 Conversions ========== //
1164 public static int ToInt32 (bool value)
1166 return value ? 1 : 0;
1169 public static int ToInt32 (byte value)
1171 return (int)value;
1174 public static int ToInt32 (char value)
1176 return (int)value;
1179 public static int ToInt32 (DateTime value)
1181 throw new InvalidCastException ("This conversion is not supported.");
1184 public static int ToInt32 (decimal value)
1186 if (value > Int32.MaxValue || value < Int32.MinValue)
1187 throw new OverflowException (Locale.GetText (
1188 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1190 // Returned Even-Rounded
1191 return (int)(Math.Round (value));
1194 public static int ToInt32 (double value)
1196 if (value > Int32.MaxValue || value < Int32.MinValue)
1197 throw new OverflowException (Locale.GetText (
1198 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1200 // Returned Even-Rounded
1201 return (int)(Math.Round (value));
1204 public static int ToInt32 (float value)
1206 if (value > Int32.MaxValue || value < Int32.MinValue)
1207 throw new OverflowException (Locale.GetText (
1208 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1210 // Returned Even-Rounded, pass as a double, could just call
1211 // Convert.ToInt32 ( (double)value);
1212 return (int)(Math.Round ( (double)value));
1215 public static int ToInt32 (int value)
1217 return value;
1220 public static int ToInt32 (long value)
1222 if (value > Int32.MaxValue || value < Int32.MinValue)
1223 throw new OverflowException (Locale.GetText (
1224 "Value is greater than Int32.MaxValue or less than Int32.MinValue"));
1226 return (int)value;
1229 [CLSCompliant (false)]
1230 public static int ToInt32 (sbyte value)
1232 return (int)value;
1235 public static int ToInt32 (short value)
1237 return (int)value;
1240 public static int ToInt32 (string value)
1242 if (value == null)
1243 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1244 return Int32.Parse (value);
1247 public static int ToInt32 (string value, IFormatProvider provider)
1249 if (value == null)
1250 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1251 return Int32.Parse (value, provider);
1255 public static int ToInt32 (string value, int fromBase)
1257 return ConvertFromBase (value, fromBase, false);
1260 [CLSCompliant (false)]
1261 public static int ToInt32 (uint value)
1263 if (value > Int32.MaxValue)
1264 throw new OverflowException (Locale.GetText (
1265 "Value is greater than Int32.MaxValue"));
1267 return (int)value;
1270 [CLSCompliant (false)]
1271 public static int ToInt32 (ulong value)
1273 if (value > Int32.MaxValue)
1274 throw new OverflowException (Locale.GetText (
1275 "Value is greater than Int32.MaxValue"));
1277 return (int)value;
1280 [CLSCompliant (false)]
1281 public static int ToInt32 (ushort value)
1283 return (int)value;
1286 public static int ToInt32 (object value)
1288 if (value == null)
1289 return 0;
1290 return ToInt32 (value, null);
1293 public static int ToInt32 (object value, IFormatProvider provider)
1295 if (value == null)
1296 return 0;
1297 return ((IConvertible) value).ToInt32 (provider);
1300 // ========== Int64 Conversions ========== //
1302 public static long ToInt64 (bool value)
1304 return value ? 1 : 0;
1307 public static long ToInt64 (byte value)
1309 return (long)(ulong)value;
1312 public static long ToInt64 (char value)
1314 return (long)value;
1317 public static long ToInt64 (DateTime value)
1319 throw new InvalidCastException ("This conversion is not supported.");
1322 public static long ToInt64 (decimal value)
1324 if (value > Int64.MaxValue || value < Int64.MinValue)
1325 throw new OverflowException (Locale.GetText (
1326 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1328 // Returned Even-Rounded
1329 return (long)(Math.Round (value));
1332 public static long ToInt64 (double value)
1334 if (value > Int64.MaxValue || value < Int64.MinValue)
1335 throw new OverflowException (Locale.GetText (
1336 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1338 // Returned Even-Rounded
1339 return (long)(Math.Round (value));
1342 public static long ToInt64 (float value)
1344 if (value > Int64.MaxValue || value < Int64.MinValue)
1345 throw new OverflowException (Locale.GetText (
1346 "Value is greater than Int64.MaxValue or less than Int64.MinValue"));
1348 // Returned Even-Rounded, pass to Math as a double, could
1349 // just call Convert.ToInt64 ( (double)value);
1350 return (long)(Math.Round ( (double)value));
1353 public static long ToInt64 (int value)
1355 return (long)value;
1358 public static long ToInt64 (long value)
1360 return value;
1363 [CLSCompliant (false)]
1364 public static long ToInt64 (sbyte value)
1366 return (long)value;
1369 public static long ToInt64 (short value)
1371 return (long)value;
1374 public static long ToInt64 (string value)
1376 if (value == null)
1377 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1378 return Int64.Parse (value);
1381 public static long ToInt64 (string value, IFormatProvider provider)
1383 if (value == null)
1384 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1385 return Int64.Parse (value, provider);
1388 public static long ToInt64 (string value, int fromBase)
1390 return ConvertFromBase64 (value, fromBase, false);
1393 [CLSCompliant (false)]
1394 public static long ToInt64 (uint value)
1396 return (long)(ulong)value;
1399 [CLSCompliant (false)]
1400 public static long ToInt64 (ulong value)
1402 if (value > Int64.MaxValue)
1403 throw new OverflowException (Locale.GetText (
1404 "Value is greater than Int64.MaxValue"));
1406 return (long)value;
1409 [CLSCompliant (false)]
1410 public static long ToInt64 (ushort value)
1412 return (long)(ulong)value;
1415 public static long ToInt64 (object value)
1417 if (value == null)
1418 return 0;
1419 return ToInt64 (value, null);
1422 public static long ToInt64 (object value, IFormatProvider provider)
1424 if (value == null)
1425 return 0;
1426 return ((IConvertible) value).ToInt64 (provider);
1429 // ========== SByte Conversions ========== //
1431 [CLSCompliant (false)]
1432 public static sbyte ToSByte (bool value)
1434 return (sbyte)(value ? 1 : 0);
1437 [CLSCompliant (false)]
1438 public static sbyte ToSByte (byte value)
1440 if (value > SByte.MaxValue)
1441 throw new OverflowException (Locale.GetText (
1442 "Value is greater than SByte.MaxValue"));
1444 return (sbyte)value;
1447 [CLSCompliant (false)]
1448 public static sbyte ToSByte (char value)
1450 if (value > SByte.MaxValue)
1451 throw new OverflowException (Locale.GetText (
1452 "Value is greater than SByte.MaxValue"));
1454 return (sbyte)value;
1457 [CLSCompliant (false)]
1458 public static sbyte ToSByte (DateTime value)
1460 throw new InvalidCastException ("This conversion is not supported.");
1463 [CLSCompliant (false)]
1464 public static sbyte ToSByte (decimal value)
1466 if (value > SByte.MaxValue || value < SByte.MinValue)
1467 throw new OverflowException (Locale.GetText (
1468 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1470 // Returned Even-Rounded
1471 return (sbyte)(Math.Round (value));
1474 [CLSCompliant (false)]
1475 public static sbyte ToSByte (double value)
1477 if (value > SByte.MaxValue || value < SByte.MinValue)
1478 throw new OverflowException (Locale.GetText (
1479 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1481 // Returned Even-Rounded
1482 return (sbyte)(Math.Round (value));
1485 [CLSCompliant (false)]
1486 public static sbyte ToSByte (float value)
1488 if (value > SByte.MaxValue || value < SByte.MinValue)
1489 throw new OverflowException (Locale.GetText (
1490 "Value is greater than SByte.MaxValue or less than SByte.Minalue"));
1492 // Returned Even-Rounded, pass as double to Math
1493 return (sbyte)(Math.Round ( (double)value));
1496 [CLSCompliant (false)]
1497 public static sbyte ToSByte (int value)
1499 if (value > SByte.MaxValue || value < SByte.MinValue)
1500 throw new OverflowException (Locale.GetText (
1501 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1503 return (sbyte)value;
1506 [CLSCompliant (false)]
1507 public static sbyte ToSByte (long value)
1509 if (value > SByte.MaxValue || value < SByte.MinValue)
1510 throw new OverflowException (Locale.GetText (
1511 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1513 return (sbyte)value;
1516 [CLSCompliant (false)]
1517 public static sbyte ToSByte (sbyte value)
1519 return value;
1522 [CLSCompliant (false)]
1523 public static sbyte ToSByte (short value)
1525 if (value > SByte.MaxValue || value < SByte.MinValue)
1526 throw new OverflowException (Locale.GetText (
1527 "Value is greater than SByte.MaxValue or less than SByte.MinValue"));
1529 return (sbyte)value;
1532 [CLSCompliant (false)]
1533 public static sbyte ToSByte (string value)
1535 if (value == null)
1536 return 0; // LAMESPEC: Spec says throw ArgumentNullException
1537 return SByte.Parse (value);
1540 [CLSCompliant (false)]
1541 public static sbyte ToSByte (string value, IFormatProvider provider)
1543 if (value == null)
1544 throw new ArgumentNullException ("value");
1545 return SByte.Parse (value, provider);
1548 [CLSCompliant (false)]
1549 public static sbyte ToSByte (string value, int fromBase)
1551 int result = ConvertFromBase (value, fromBase, false);
1552 if (fromBase != 10) {
1553 // note: no sign are available to detect negatives
1554 if (result > SByte.MaxValue) {
1555 // return negative 2's complement
1556 return Convert.ToSByte (-(256 - result));
1559 return Convert.ToSByte (result);
1562 [CLSCompliant (false)]
1563 public static sbyte ToSByte (uint value)
1565 if (value > SByte.MaxValue)
1566 throw new OverflowException (Locale.GetText (
1567 "Value is greater than SByte.MaxValue"));
1569 return (sbyte)value;
1572 [CLSCompliant (false)]
1573 public static sbyte ToSByte (ulong value)
1575 if (value > (ulong)SByte.MaxValue)
1576 throw new OverflowException (Locale.GetText (
1577 "Value is greater than SByte.MaxValue"));
1579 return (sbyte)value;
1582 [CLSCompliant (false)]
1583 public static sbyte ToSByte (ushort value)
1585 if (value > SByte.MaxValue)
1586 throw new OverflowException (Locale.GetText (
1587 "Value is greater than SByte.MaxValue"));
1589 return (sbyte)value;
1592 [CLSCompliant (false)]
1593 public static sbyte ToSByte (object value)
1595 if (value == null)
1596 return 0;
1597 return ToSByte (value, null);
1600 [CLSCompliant (false)]
1601 public static sbyte ToSByte (object value, IFormatProvider provider)
1603 if (value == null)
1604 return 0;
1605 return ((IConvertible) value).ToSByte (provider);
1608 // ========== Single Conversions ========== //
1610 public static float ToSingle (bool value)
1612 return value ? 1 : 0;
1615 public static float ToSingle (byte value)
1617 return (float)value;
1620 public static float ToSingle (Char value)
1622 throw new InvalidCastException ("This conversion is not supported.");
1625 public static float ToSingle (DateTime value)
1627 throw new InvalidCastException ("This conversion is not supported.");
1630 public static float ToSingle (decimal value)
1632 return (float)value;
1635 public static float ToSingle (double value)
1637 return (float)value;
1640 public static float ToSingle (float value)
1642 return value;
1645 public static float ToSingle (int value)
1647 return (float)value;
1650 public static float ToSingle (long value)
1652 return (float)value;
1655 [CLSCompliant (false)]
1656 public static float ToSingle (sbyte value)
1658 return (float)value;
1661 public static float ToSingle (short value)
1663 return (float)value;
1666 public static float ToSingle (string value)
1668 if (value == null)
1669 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1670 return Single.Parse (value);
1673 public static float ToSingle (string value, IFormatProvider provider)
1675 if (value == null)
1676 return 0.0f; // LAMESPEC: Spec says throw ArgumentNullException
1677 return Single.Parse (value, provider);
1680 [CLSCompliant (false)]
1681 public static float ToSingle (uint value)
1683 return (float)value;
1686 [CLSCompliant (false)]
1687 public static float ToSingle (ulong value)
1689 return (float)value;
1692 [CLSCompliant (false)]
1693 public static float ToSingle (ushort value)
1695 return (float)value;
1698 public static float ToSingle (object value)
1700 if (value == null)
1701 return 0.0f;
1702 return ToSingle (value, null);
1705 // [CLSCompliant (false)]
1706 public static float ToSingle (object value, IFormatProvider provider)
1708 if (value == null)
1709 return 0.0f;
1710 return ((IConvertible) value).ToSingle (provider);
1713 // ========== String Conversions ========== //
1715 public static string ToString (bool value)
1717 return value.ToString ();
1720 public static string ToString (bool value, IFormatProvider provider)
1722 return value.ToString (); // the same as ToString (bool).
1725 public static string ToString (byte value)
1727 return value.ToString ();
1730 public static string ToString (byte value, IFormatProvider provider)
1732 return value.ToString (provider);
1735 public static string ToString (byte value, int toBase)
1737 if (value == 0)
1738 return "0";
1739 if (toBase == 10)
1740 return value.ToString ();
1742 byte[] val = BitConverter.GetBytes (value);
1744 switch (toBase) {
1745 case 2:
1746 return ConvertToBase2 (val);
1747 case 8:
1748 return ConvertToBase8 (val);
1749 case 16:
1750 return ConvertToBase16 (val);
1751 default:
1752 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1756 public static string ToString (char value)
1758 return value.ToString ();
1761 public static string ToString (char value, IFormatProvider provider)
1763 return value.ToString (); // the same as ToString (char)
1766 public static string ToString (DateTime value)
1768 return value.ToString ();
1771 public static string ToString (DateTime value, IFormatProvider provider)
1773 return value.ToString (provider);
1776 public static string ToString (decimal value)
1778 return value.ToString ();
1781 public static string ToString (decimal value, IFormatProvider provider)
1783 return value.ToString (provider);
1786 public static string ToString (double value)
1788 return value.ToString ();
1791 public static string ToString (double value, IFormatProvider provider)
1793 return value.ToString (provider);
1796 public static string ToString (float value)
1798 return value.ToString ();
1801 public static string ToString (float value, IFormatProvider provider)
1803 return value.ToString (provider);
1806 public static string ToString (int value)
1808 return value.ToString ();
1811 public static string ToString (int value, int toBase)
1813 if (value == 0)
1814 return "0";
1815 if (toBase == 10)
1816 return value.ToString ();
1818 byte[] val = BitConverter.GetBytes (value);
1820 switch (toBase) {
1821 case 2:
1822 return ConvertToBase2 (val);
1823 case 8:
1824 return ConvertToBase8 (val);
1825 case 16:
1826 return ConvertToBase16 (val);
1827 default:
1828 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1832 public static string ToString (int value, IFormatProvider provider)
1834 return value.ToString (provider);
1837 public static string ToString (long value)
1839 return value.ToString ();
1842 public static string ToString (long value, int toBase)
1844 if (value == 0)
1845 return "0";
1846 if (toBase == 10)
1847 return value.ToString ();
1849 byte[] val = BitConverter.GetBytes (value);
1851 switch (toBase) {
1852 case 2:
1853 return ConvertToBase2 (val);
1854 case 8:
1855 return ConvertToBase8 (val);
1856 case 16:
1857 return ConvertToBase16 (val);
1858 default:
1859 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1863 public static string ToString (long value, IFormatProvider provider)
1865 return value.ToString (provider);
1868 public static string ToString (object value)
1870 return ToString (value, null);
1873 public static string ToString (object value, IFormatProvider provider)
1875 if (value is IConvertible)
1876 return ((IConvertible) value).ToString (provider);
1877 else if (value != null)
1878 return value.ToString ();
1879 return String.Empty;
1882 [CLSCompliant (false)]
1883 public static string ToString (sbyte value)
1885 return value.ToString ();
1888 [CLSCompliant (false)]
1889 public static string ToString (sbyte value, IFormatProvider provider)
1891 return value.ToString (provider);
1894 public static string ToString (short value)
1896 return value.ToString ();
1899 public static string ToString (short value, int toBase)
1901 if (value == 0)
1902 return "0";
1903 if (toBase == 10)
1904 return value.ToString ();
1906 byte[] val = BitConverter.GetBytes (value);
1908 switch (toBase) {
1909 case 2:
1910 return ConvertToBase2 (val);
1911 case 8:
1912 return ConvertToBase8 (val);
1913 case 16:
1914 return ConvertToBase16 (val);
1915 default:
1916 throw new ArgumentException (Locale.GetText ("toBase is not valid."));
1920 public static string ToString (short value, IFormatProvider provider)
1922 return value.ToString (provider);
1925 public static string ToString (string value)
1927 return value;
1930 public static string ToString (string value, IFormatProvider provider)
1932 return value; // provider is ignored.
1935 [CLSCompliant (false)]
1936 public static string ToString (uint value)
1938 return value.ToString ();
1941 [CLSCompliant (false)]
1942 public static string ToString (uint value, IFormatProvider provider)
1944 return value.ToString (provider);
1947 [CLSCompliant (false)]
1948 public static string ToString (ulong value)
1950 return value.ToString ();
1953 [CLSCompliant (false)]
1954 public static string ToString (ulong value, IFormatProvider provider)
1956 return value.ToString (provider);
1959 [CLSCompliant (false)]
1960 public static string ToString (ushort value)
1962 return value.ToString ();
1965 [CLSCompliant (false)]
1966 public static string ToString (ushort value, IFormatProvider provider)
1968 return value.ToString (provider);
1971 // ========== UInt16 Conversions ========== //
1973 [CLSCompliant (false)]
1974 public static ushort ToUInt16 (bool value)
1976 return (ushort)(value ? 1 : 0);
1979 [CLSCompliant (false)]
1980 public static ushort ToUInt16 (byte value)
1982 return (ushort)value;
1985 [CLSCompliant (false)]
1986 public static ushort ToUInt16 (char value)
1988 return (ushort)value;
1991 [CLSCompliant (false)]
1992 public static ushort ToUInt16 (DateTime value)
1994 throw new InvalidCastException ("This conversion is not supported.");
1997 [CLSCompliant (false)]
1998 public static ushort ToUInt16 (decimal value)
2000 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2001 throw new OverflowException (Locale.GetText (
2002 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2004 // Returned Even-Rounded
2005 return (ushort)(Math.Round (value));
2008 [CLSCompliant (false)]
2009 public static ushort ToUInt16 (double value)
2011 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2012 throw new OverflowException (Locale.GetText (
2013 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2015 // Returned Even-Rounded
2016 return (ushort)(Math.Round (value));
2019 [CLSCompliant (false)]
2020 public static ushort ToUInt16 (float value)
2022 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2023 throw new OverflowException (Locale.GetText (
2024 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2026 // Returned Even-Rounded, pass as double to Math
2027 return (ushort)(Math.Round ( (double)value));
2030 [CLSCompliant (false)]
2031 public static ushort ToUInt16 (int value)
2033 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2034 throw new OverflowException (Locale.GetText (
2035 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2037 return (ushort)value;
2040 [CLSCompliant (false)]
2041 public static ushort ToUInt16 (long value)
2043 if (value > UInt16.MaxValue || value < UInt16.MinValue)
2044 throw new OverflowException (Locale.GetText (
2045 "Value is greater than UInt16.MaxValue or less than UInt16.MinValue"));
2047 return (ushort)value;
2050 [CLSCompliant (false)]
2051 public static ushort ToUInt16 (sbyte value)
2053 if (value < UInt16.MinValue)
2054 throw new OverflowException (Locale.GetText (
2055 "Value is less than UInt16.MinValue"));
2057 return (ushort)value;
2060 [CLSCompliant (false)]
2061 public static ushort ToUInt16 (short value)
2063 if (value < UInt16.MinValue)
2064 throw new OverflowException (Locale.GetText (
2065 "Value is less than UInt16.MinValue"));
2067 return (ushort)value;
2070 [CLSCompliant (false)]
2071 public static ushort ToUInt16 (string value)
2073 if (value == null)
2074 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2075 return UInt16.Parse (value);
2078 [CLSCompliant (false)]
2079 public static ushort ToUInt16 (string value, IFormatProvider provider)
2081 if (value == null)
2082 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2083 return UInt16.Parse (value, provider);
2086 [CLSCompliant (false)]
2087 public static ushort ToUInt16 (string value, int fromBase)
2089 return ToUInt16 (ConvertFromBase (value, fromBase, true));
2092 [CLSCompliant (false)]
2093 public static ushort ToUInt16 (uint value)
2095 if (value > UInt16.MaxValue)
2096 throw new OverflowException (Locale.GetText (
2097 "Value is greater than UInt16.MaxValue"));
2099 return (ushort)value;
2102 [CLSCompliant (false)]
2103 public static ushort ToUInt16 (ulong value)
2105 if (value > (ulong)UInt16.MaxValue)
2106 throw new OverflowException (Locale.GetText (
2107 "Value is greater than UInt16.MaxValue"));
2109 return (ushort)value;
2112 [CLSCompliant (false)]
2113 public static ushort ToUInt16 (ushort value)
2115 return value;
2118 [CLSCompliant (false)]
2119 public static ushort ToUInt16 (object value)
2121 if (value == null)
2122 return 0;
2123 return ToUInt16 (value, null);
2126 [CLSCompliant (false)]
2127 public static ushort ToUInt16 (object value, IFormatProvider provider)
2129 if (value == null)
2130 return 0;
2131 return ((IConvertible) value).ToUInt16 (provider);
2134 // ========== UInt32 Conversions ========== //
2136 [CLSCompliant (false)]
2137 public static uint ToUInt32 (bool value)
2139 return (uint)(value ? 1 : 0);
2142 [CLSCompliant (false)]
2143 public static uint ToUInt32 (byte value)
2145 return (uint)value;
2148 [CLSCompliant (false)]
2149 public static uint ToUInt32 (char value)
2151 return (uint)value;
2154 [CLSCompliant (false)]
2155 public static uint ToUInt32 (DateTime value)
2157 throw new InvalidCastException ("This conversion is not supported.");
2160 [CLSCompliant (false)]
2161 public static uint ToUInt32 (decimal value)
2163 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2164 throw new OverflowException (Locale.GetText (
2165 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2167 // Returned Even-Rounded
2168 return (uint)(Math.Round (value));
2171 [CLSCompliant (false)]
2172 public static uint ToUInt32 (double value)
2174 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2175 throw new OverflowException (Locale.GetText (
2176 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2178 // Returned Even-Rounded
2179 return (uint)(Math.Round (value));
2182 [CLSCompliant (false)]
2183 public static uint ToUInt32 (float value)
2185 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2186 throw new OverflowException (Locale.GetText (
2187 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2189 // Returned Even-Rounded, pass as double to Math
2190 return (uint)(Math.Round ( (double)value));
2193 [CLSCompliant (false)]
2194 public static uint ToUInt32 (int value)
2196 if (value < UInt32.MinValue)
2197 throw new OverflowException (Locale.GetText (
2198 "Value is less than UInt32.MinValue"));
2200 return (uint)value;
2203 [CLSCompliant (false)]
2204 public static uint ToUInt32 (long value)
2206 if (value > UInt32.MaxValue || value < UInt32.MinValue)
2207 throw new OverflowException (Locale.GetText (
2208 "Value is greater than UInt32.MaxValue or less than UInt32.MinValue"));
2210 return (uint)value;
2213 [CLSCompliant (false)]
2214 public static uint ToUInt32 (sbyte value)
2216 if (value < UInt32.MinValue)
2217 throw new OverflowException (Locale.GetText (
2218 "Value is less than UInt32.MinValue"));
2220 return (uint)value;
2223 [CLSCompliant (false)]
2224 public static uint ToUInt32 (short value)
2226 if (value < UInt32.MinValue)
2227 throw new OverflowException (Locale.GetText (
2228 "Value is less than UInt32.MinValue"));
2230 return (uint)value;
2233 [CLSCompliant (false)]
2234 public static uint ToUInt32 (string value)
2236 if (value == null)
2237 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2238 return UInt32.Parse (value);
2241 [CLSCompliant (false)]
2242 public static uint ToUInt32 (string value, IFormatProvider provider)
2244 if (value == null)
2245 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2246 return UInt32.Parse (value, provider);
2249 [CLSCompliant (false)]
2250 public static uint ToUInt32 (string value, int fromBase)
2252 return (uint) ConvertFromBase (value, fromBase, true);
2255 [CLSCompliant (false)]
2256 public static uint ToUInt32 (uint value)
2258 return value;
2261 [CLSCompliant (false)]
2262 public static uint ToUInt32 (ulong value)
2264 if (value > UInt32.MaxValue)
2265 throw new OverflowException (Locale.GetText (
2266 "Value is greater than UInt32.MaxValue"));
2268 return (uint)value;
2271 [CLSCompliant (false)]
2272 public static uint ToUInt32 (ushort value)
2274 return (uint)value;
2277 [CLSCompliant (false)]
2278 public static uint ToUInt32 (object value)
2280 if (value == null)
2281 return 0;
2282 return ToUInt32 (value, null);
2285 [CLSCompliant (false)]
2286 public static uint ToUInt32 (object value, IFormatProvider provider)
2288 if (value == null)
2289 return 0;
2290 return ((IConvertible) value).ToUInt32 (provider);
2294 // ========== UInt64 Conversions ========== //
2296 [CLSCompliant (false)]
2297 public static ulong ToUInt64 (bool value)
2299 return (ulong)(value ? 1 : 0);
2302 [CLSCompliant (false)]
2303 public static ulong ToUInt64 (byte value)
2305 return (ulong)value;
2308 [CLSCompliant (false)]
2309 public static ulong ToUInt64 (char value)
2311 return (ulong)value;
2314 [CLSCompliant (false)]
2315 public static ulong ToUInt64 (DateTime value)
2317 throw new InvalidCastException ("The conversion is not supported.");
2320 [CLSCompliant (false)]
2321 public static ulong ToUInt64 (decimal value)
2323 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2324 throw new OverflowException (Locale.GetText (
2325 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2327 // Returned Even-Rounded
2328 return (ulong)(Math.Round (value));
2331 [CLSCompliant (false)]
2332 public static ulong ToUInt64 (double value)
2334 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2335 throw new OverflowException (Locale.GetText (
2336 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2338 // Returned Even-Rounded
2339 return (ulong)(Math.Round (value));
2342 [CLSCompliant (false)]
2343 public static ulong ToUInt64 (float value)
2345 if (value > UInt64.MaxValue || value < UInt64.MinValue)
2346 throw new OverflowException (Locale.GetText (
2347 "Value is greater than UInt64.MaxValue or less than UInt64.MinValue"));
2349 // Returned Even-Rounded, pass as a double to Math
2350 return (ulong)(Math.Round ( (double)value));
2353 [CLSCompliant (false)]
2354 public static ulong ToUInt64 (int value)
2356 if (value < (int)UInt64.MinValue)
2357 throw new OverflowException (Locale.GetText (
2358 "Value is less than UInt64.MinValue"));
2360 return (ulong)value;
2363 [CLSCompliant (false)]
2364 public static ulong ToUInt64 (long value)
2366 if (value < (long)UInt64.MinValue)
2367 throw new OverflowException (Locale.GetText (
2368 "Value is less than UInt64.MinValue"));
2370 return (ulong)value;
2373 [CLSCompliant (false)]
2374 public static ulong ToUInt64 (sbyte value)
2376 if (value < (sbyte)UInt64.MinValue)
2377 throw new OverflowException
2378 ("Value is less than UInt64.MinValue");
2380 return (ulong)value;
2383 [CLSCompliant (false)]
2384 public static ulong ToUInt64 (short value)
2386 if (value < (short)UInt64.MinValue)
2387 throw new OverflowException (Locale.GetText (
2388 "Value is less than UInt64.MinValue"));
2390 return (ulong)value;
2393 [CLSCompliant (false)]
2394 public static ulong ToUInt64 (string value)
2396 if (value == null)
2397 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2398 return UInt64.Parse (value);
2401 [CLSCompliant (false)]
2402 public static ulong ToUInt64 (string value, IFormatProvider provider)
2404 if (value == null)
2405 return 0; // LAMESPEC: Spec says throw ArgumentNullException
2406 return UInt64.Parse (value, provider);
2409 [CLSCompliant (false)]
2410 public static ulong ToUInt64 (string value, int fromBase)
2412 return (ulong) ConvertFromBase (value, fromBase, true);
2415 [CLSCompliant (false)]
2416 public static ulong ToUInt64 (uint value)
2418 return (ulong)value;
2421 [CLSCompliant (false)]
2422 public static ulong ToUInt64 (ulong value)
2424 return value;
2427 [CLSCompliant (false)]
2428 public static ulong ToUInt64 (ushort value)
2430 return (ulong)value;
2433 [CLSCompliant (false)]
2434 public static ulong ToUInt64 (object value)
2436 if (value == null)
2437 return 0;
2438 return ToUInt64 (value, null);
2441 [CLSCompliant (false)]
2442 public static ulong ToUInt64 (object value, IFormatProvider provider)
2444 if (value == null)
2445 return 0;
2446 return ((IConvertible) value).ToUInt64 (provider);
2450 // ========== Conversion / Helper Functions ========== //
2452 public static object ChangeType (object value, Type conversionType)
2454 if ((value != null) && (conversionType == null))
2455 throw new ArgumentNullException ("conversionType");
2456 CultureInfo ci = CultureInfo.CurrentCulture;
2457 NumberFormatInfo number = ci.NumberFormat;
2458 return ToType (value, conversionType, number);
2461 public static object ChangeType (object value, TypeCode typeCode)
2463 CultureInfo ci = CultureInfo.CurrentCulture;
2464 Type conversionType = conversionTable [(int) typeCode];
2465 NumberFormatInfo number = ci.NumberFormat;
2466 return ToType (value, conversionType, number);
2469 public static object ChangeType (object value, Type conversionType, IFormatProvider provider)
2471 if ((value != null) && (conversionType == null))
2472 throw new ArgumentNullException ("conversionType");
2473 return ToType (value, conversionType, provider);
2476 public static object ChangeType (object value, TypeCode typeCode, IFormatProvider provider)
2478 Type conversionType = conversionTable [(int)typeCode];
2479 return ToType (value, conversionType, provider);
2482 private static bool NotValidBase (int value)
2484 if ((value == 2) || (value == 8) ||
2485 (value == 10) || (value == 16))
2486 return false;
2488 return true;
2491 private static int ConvertFromBase (string value, int fromBase, bool unsigned)
2493 if (NotValidBase (fromBase))
2494 throw new ArgumentException ("fromBase is not valid.");
2495 if (value == null)
2496 return 0;
2498 int chars = 0;
2499 int result = 0;
2500 int digitValue;
2502 int i=0;
2503 int len = value.Length;
2504 bool negative = false;
2506 // special processing for some bases
2507 switch (fromBase) {
2508 case 10:
2509 if (value[i] == '-') {
2510 if (unsigned) {
2511 throw new OverflowException (
2512 Locale.GetText ("Cannot convert negative to unsigned"));
2514 negative = true;
2515 i++;
2517 break;
2518 case 16:
2519 if (len >= i+2) {
2520 // 0x00 or 0X00
2521 if ((value[i] == '0') && ((value [i+1] == 'x') || (value [i+1] == 'X'))) {
2522 i+=2;
2524 if (len == i) {
2525 throw new FormatException (
2526 Locale.GetText ("Missing number after prefix"));
2529 break;
2532 if (value[i] == '+') {
2533 i++;
2536 while(i<len) {
2537 char c = value[i++];
2538 if (Char.IsNumber (c))
2539 digitValue = c - '0';
2540 else if (Char.IsLetter (c))
2541 digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2542 else if (c == '-')
2543 throw new ArgumentException ("Negative are valid only for base 10");
2544 else
2545 throw new FormatException ("This is an invalid string: " + value);
2547 if (digitValue >= fromBase)
2548 throw new FormatException ("the digits are invalid.");
2550 result = (fromBase) * result + digitValue;
2551 chars ++;
2555 if (chars == 0)
2556 throw new FormatException ("Could not find any digits.");
2558 if (result > Int32.MaxValue || result < Int32.MinValue)
2559 throw new OverflowException ("There is an overflow.");
2561 if (negative)
2562 return -result;
2563 else
2564 return result;
2567 // note: this has nothing to do with base64 encoding (just base and Int64)
2568 private static long ConvertFromBase64 (string value, int fromBase, bool unsigned)
2570 if (NotValidBase (fromBase))
2571 throw new ArgumentException ("fromBase is not valid.");
2572 if (value == null)
2573 return 0;
2575 int chars = 0;
2576 int digitValue = -1;
2577 uint Base = (uint) fromBase;
2578 ulong result = 0;
2579 bool negative = false;
2581 foreach (char c in value) {
2582 if (Char.IsNumber (c))
2583 digitValue = c - '0';
2584 else if (Char.IsLetter (c))
2585 digitValue = Char.ToLowerInvariant (c) - 'a' + 10;
2586 else if ((c == '-') && (!unsigned)) {
2587 if (fromBase != 10)
2588 throw new ArgumentException ("Negative are valid only for base 10");
2589 negative = true;
2590 continue;
2592 else
2593 throw new FormatException ("This is an invalid string: " + value);
2595 if (digitValue >= fromBase)
2596 throw new FormatException ("the digits are invalid.");
2598 result = (ulong) (Base * result + (uint)digitValue);
2599 chars ++;
2602 if (chars == 0)
2603 throw new FormatException ("Could not find any digits.");
2605 if (negative)
2606 return -1 * (long) result;
2607 else
2608 return (long) result;
2611 private static void EndianSwap (ref byte[] value)
2613 byte[] buf = new byte[value.Length];
2614 for (int i = 0; i < value.Length; i++)
2615 buf[i] = value[value.Length-1-i];
2616 value = buf;
2619 private static string ConvertToBase2 (byte[] value)
2621 if (!BitConverter.IsLittleEndian)
2622 EndianSwap (ref value);
2623 StringBuilder sb = new StringBuilder ();
2624 for (int i = value.Length - 1; i >= 0; i--) {
2625 byte b = value [i];
2626 for (int j = 0; j < 8; j++) {
2627 if ((b & 0x80) == 0x80) {
2628 sb.Append ('1');
2630 else {
2631 if (sb.Length > 0)
2632 sb.Append ('0');
2634 b <<= 1;
2637 return sb.ToString ();
2640 private static string ConvertToBase8 (byte[] value)
2642 if (!BitConverter.IsLittleEndian)
2643 EndianSwap (ref value);
2644 ulong l = 0;
2645 switch (value.Length) {
2646 case 1:
2647 l = (ulong) value [0];
2648 break;
2649 case 2:
2650 l = (ulong) BitConverter.ToUInt16 (value, 0);
2651 break;
2652 case 4:
2653 l = (ulong) BitConverter.ToUInt32 (value, 0);
2654 break;
2655 case 8:
2656 l = BitConverter.ToUInt64 (value, 0);
2657 break;
2658 default:
2659 throw new ArgumentException ("value");
2662 StringBuilder sb = new StringBuilder ();
2663 for (int i = 21; i >= 0; i--) {
2664 // 3 bits at the time
2665 char val = (char) ((l >> i * 3) & 0x7);
2666 if ((val != 0) || (sb.Length > 0)) {
2667 val += '0';
2668 sb.Append (val);
2671 return sb.ToString ();
2674 private static string ConvertToBase16 (byte[] value)
2676 if (!BitConverter.IsLittleEndian)
2677 EndianSwap (ref value);
2678 StringBuilder sb = new StringBuilder ();
2679 for (int i = value.Length - 1; i >= 0; i--) {
2680 char high = (char)((value[i] >> 4) & 0x0f);
2681 if ((high != 0) || (sb.Length > 0)) {
2682 if (high < 10)
2683 high += '0';
2684 else {
2685 high -= (char) 10;
2686 high += 'a';
2688 sb.Append (high);
2691 char low = (char)(value[i] & 0x0f);
2692 if ((low != 0) || (sb.Length > 0)) {
2693 if (low < 10)
2694 low += '0';
2695 else {
2696 low -= (char) 10;
2697 low += 'a';
2699 sb.Append (low);
2702 return sb.ToString ();
2705 // Lookup table for the conversion ToType method. Order
2706 // is important! Used by ToType for comparing the target
2707 // type, and uses hardcoded array indexes.
2708 private static readonly Type[] conversionTable = {
2709 // Valid ICovnertible Types
2710 null, // 0 empty
2711 typeof (object), // 1 TypeCode.Object
2712 typeof (DBNull), // 2 TypeCode.DBNull
2713 typeof (Boolean), // 3 TypeCode.Boolean
2714 typeof (Char), // 4 TypeCode.Char
2715 typeof (SByte), // 5 TypeCode.SByte
2716 typeof (Byte), // 6 TypeCode.Byte
2717 typeof (Int16), // 7 TypeCode.Int16
2718 typeof (UInt16), // 8 TypeCode.UInt16
2719 typeof (Int32), // 9 TypeCode.Int32
2720 typeof (UInt32), // 10 TypeCode.UInt32
2721 typeof (Int64), // 11 TypeCode.Int64
2722 typeof (UInt64), // 12 TypeCode.UInt64
2723 typeof (Single), // 13 TypeCode.Single
2724 typeof (Double), // 14 TypeCode.Double
2725 typeof (Decimal), // 15 TypeCode.Decimal
2726 typeof (DateTime), // 16 TypeCode.DateTime
2727 null, // 17 null.
2728 typeof (String), // 18 TypeCode.String
2731 // Function to convert an object to another type and return
2732 // it as an object. In place for the core data types to use
2733 // when implementing IConvertible. Uses hardcoded indexes in
2734 // the conversionTypes array, so if modify carefully.
2735 internal static object ToType (object value, Type conversionType,
2736 IFormatProvider provider)
2738 if (value == null) {
2739 if ((conversionType != null) && conversionType.IsValueType)
2740 throw new InvalidCastException ("Null object can not be converted to a value type.");
2741 else
2742 return null;
2745 if (conversionType == null)
2746 throw new InvalidCastException ("Cannot cast to destination type.");
2748 if (value.GetType () == conversionType)
2749 return value;
2751 if (value is IConvertible) {
2752 IConvertible convertValue = (IConvertible) value;
2754 if (conversionType == conversionTable[0]) // 0 Empty
2755 throw new ArgumentNullException ();
2757 else if (conversionType == conversionTable[1]) // 1 TypeCode.Object
2758 return (object) value;
2760 else if (conversionType == conversionTable[2]) // 2 TypeCode.DBNull
2761 throw new InvalidCastException (
2762 "Cannot cast to DBNull, it's not IConvertible");
2764 else if (conversionType == conversionTable[3]) // 3 TypeCode.Boolean
2765 return (object) convertValue.ToBoolean (provider);
2767 else if (conversionType == conversionTable[4]) // 4 TypeCode.Char
2768 return (object) convertValue.ToChar (provider);
2770 else if (conversionType == conversionTable[5]) // 5 TypeCode.SByte
2771 return (object) convertValue.ToSByte (provider);
2773 else if (conversionType == conversionTable[6]) // 6 TypeCode.Byte
2774 return (object) convertValue.ToByte (provider);
2776 else if (conversionType == conversionTable[7]) // 7 TypeCode.Int16
2777 return (object) convertValue.ToInt16 (provider);
2779 else if (conversionType == conversionTable[8]) // 8 TypeCode.UInt16
2780 return (object) convertValue.ToUInt16 (provider);
2782 else if (conversionType == conversionTable[9]) // 9 TypeCode.Int32
2783 return (object) convertValue.ToInt32 (provider);
2785 else if (conversionType == conversionTable[10]) // 10 TypeCode.UInt32
2786 return (object) convertValue.ToUInt32 (provider);
2788 else if (conversionType == conversionTable[11]) // 11 TypeCode.Int64
2789 return (object) convertValue.ToInt64 (provider);
2791 else if (conversionType == conversionTable[12]) // 12 TypeCode.UInt64
2792 return (object) convertValue.ToUInt64 (provider);
2794 else if (conversionType == conversionTable[13]) // 13 TypeCode.Single
2795 return (object) convertValue.ToSingle (provider);
2797 else if (conversionType == conversionTable[14]) // 14 TypeCode.Double
2798 return (object) convertValue.ToDouble (provider);
2800 else if (conversionType == conversionTable[15]) // 15 TypeCode.Decimal
2801 return (object) convertValue.ToDecimal (provider);
2803 else if (conversionType == conversionTable[16]) // 16 TypeCode.DateTime
2804 return (object) convertValue.ToDateTime (provider);
2806 else if (conversionType == conversionTable[18]) // 18 TypeCode.String
2807 return (object) convertValue.ToString (provider);
2808 else {
2809 throw new ArgumentException (Locale.GetText ("Unknown target conversion type"));
2811 } else
2812 // Not in the conversion table
2813 throw new InvalidCastException ((Locale.GetText (
2814 "Value is not a convertible object: " + value.GetType().ToString() + " to " + conversionType.FullName)));