2001-07-24 Derek Holden <dholden@draper.com>
[mono.git] / mcs / class / corlib / System / Convert.cs
1 //
2 // System.Convert.cs
3 //
4 // Author:
5 //   Derek Holden (dholden@draper.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
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.
15 //
16 // This header and the one above it can be formatted however, just trying
17 // to keep it consistent w/ the existing mcs headers.
18 //
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.
26 //
27 // See http://lists.ximian.com/archives/public/mono-list/2001-July/000525.html
28 //
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.
35 //
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.
41 //
42 // The .NET Framework SDK lists DBNull as a member of this class
43 // as 'public static readonly object DBNull;'. 
44 //
45 // It should also be decided if all the cast return values should be
46 // returned as unchecked or not.
47 //
48 // All the XML function comments were auto generated which is why they
49 // sound someone redundant.
50 //
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
68 //
69
70 namespace System {
71   
72         public sealed class Convert {
73         
74                 // ========== Boolean Conversions ========== //
75         
76                 public static bool ToBoolean (bool value) 
77                 { 
78                         return value; 
79                 }
80
81                 public static bool ToBoolean (byte value) 
82                 { 
83                         return (value != 0); 
84                 }
85
86                 public static bool ToBoolean (decimal value) 
87                 { 
88                         return (value != 0M); 
89                 }
90
91                 public static bool ToBoolean (double value) 
92                 { 
93                         return (value != 0); 
94                 }
95
96                 public static bool ToBoolean (float value) 
97                 { 
98                         return (value != 0f); 
99                 }
100
101                 public static bool ToBoolean (int value) 
102                 { 
103                         return (value != 0); 
104                 }
105
106                 public static bool ToBoolean (long value) 
107                 { 
108                         return (value != 0); 
109                 }
110
111                 public static bool ToBoolean (sbyte value) 
112                 { 
113                         return (value != 0); 
114                 }
115         
116                 public static bool ToBoolean (short value) 
117                 { 
118                         return (value != 0); 
119                 }
120
121                 public static bool ToBoolean (string value) 
122                 {
123                         return Boolean.Parse (value);
124                 }
125         
126                 public static bool ToBoolean (uint value) 
127                 { 
128                         return (value != 0);
129                 }
130
131                 public static bool ToBoolean (ulong value) 
132                 { 
133                         return (value != 0); 
134                 }
135
136                 public static bool ToBoolean (ushort value) 
137                 { 
138                         return (value != 0); 
139                 }
140
141                 // ========== Byte Conversions ========== //
142         
143                 public static byte ToByte (bool value) 
144                 { 
145                         return (byte)(value ? 1 : 0); 
146                 }
147         
148                 public static byte ToByte (byte value) 
149                 { 
150                         return value; 
151                 }
152
153                 public static byte ToByte (char value) 
154                 { 
155                         if (value > Byte.MaxValue)
156                                 throw new OverflowException
157                                 ("Value is greater than Byte.MaxValue");
158
159                         return (byte)value;
160                 }
161         
162                 public static byte ToByte (decimal value) 
163                 { 
164                         if (value > Byte.MaxValue || value < Byte.MinValue)
165                                 throw new OverflowException
166                                 ("Value is greater than Byte.MaxValue or less than Byte.MinValue");
167           
168                         // Returned Even-Rounded
169                         return (byte)(Math.Round (value));
170                 }
171         
172                 public static byte ToByte (double value) 
173                 { 
174                         if (value > Byte.MaxValue || value < Byte.MinValue)
175                                 throw new OverflowException
176                                 ("Value is greater than Byte.MaxValue or less than Byte.MinValue");
177           
178                         // This and the float version of ToByte are the only ones
179                         // the spec listed as checking for .NaN and Infinity overflow
180                         if (Double.IsNaN(value) || Double.IsInfinity(value))
181                                 throw new OverflowException
182                                 ("Value is equal to Double.NaN, Double.PositiveInfinity, or Double.NegativeInfinity");
183
184                         // Returned Even-Rounded
185                         return (byte)(Math.Round (value));
186                 }
187
188                 public static byte ToByte (float value) 
189                 { 
190                         if (value > Byte.MaxValue || value < Byte.MinValue)
191                                 throw new OverflowException
192                                 ("Value is greater than Byte.MaxValue or less than Byte.Minalue");
193
194                         // This and the double version of ToByte are the only ones
195                         // the spec listed as checking for .NaN and Infinity overflow
196                         if (Single.IsNaN(value) || Single.IsInfinity(value))
197                                 throw new OverflowException
198                                 ("Value is equal to Single.NaN, Single.PositiveInfinity, or Single.NegativeInfinity");
199           
200                         // Returned Even-Rounded, pass it as a double, could have this
201                         // method just call Convert.ToByte ( (double)value)
202                         return (byte)(Math.Round ( (double)value));
203                 }
204
205                 public static byte ToByte (int value) 
206                 { 
207                         if (value > Byte.MaxValue || value < Byte.MinValue)
208                                 throw new OverflowException
209                                 ("Value is greater than Byte.MaxValue or less than Byte.MinValue");
210           
211                         return (byte)value; 
212                 }
213
214                 public static byte ToByte (long value) 
215                 { 
216                         if (value > Byte.MaxValue || value < Byte.MinValue)
217                                 throw new OverflowException
218                                 ("Value is greater than Byte.MaxValue or less than Byte.MinValue");
219           
220                         return (byte)value;
221                 }
222
223                 public static byte ToByte (sbyte value) 
224                 { 
225                         if (value < Byte.MinValue)
226                                 throw new OverflowException
227                                 ("Value is less than Byte.MinValue");
228           
229                         return (byte)value;
230                 }
231         
232                 public static byte ToByte (short value) 
233                 { 
234                         if (value > Byte.MaxValue || value < Byte.MinValue)
235                                 throw new OverflowException
236                                 ("Value is greater than Byte.MaxValue or less than Byte.MinValue");
237           
238                         return (byte)value; 
239                 }
240
241                 public static byte ToByte (string value) 
242                 {
243                         return Byte.Parse (value);
244                 }
245
246                 public static byte ToByte (string value, IFormatProvider provider) 
247                 {
248                         return Byte.Parse (value, provider);
249                 }
250         
251                 public static byte ToByte (uint value) 
252                 { 
253                         if (value > Byte.MaxValue)
254                                 throw new OverflowException
255                                 ("Value is greater than Byte.MaxValue");
256
257                         return (byte)value;
258                 }
259
260                 public static byte ToByte (ulong value) 
261                 { 
262                         if (value > Byte.MaxValue)
263                                 throw new OverflowException
264                                 ("Value is greater than Byte.MaxValue");
265
266                         return (byte)value;
267                 }
268
269                 public static byte ToByte (ushort value) 
270                 { 
271                         if (value > Byte.MaxValue)
272                                 throw new OverflowException
273                                 ("Value is greater than Byte.MaxValue");
274
275                         return (byte)value;
276                 }
277
278                 // ========== Char Conversions ========== //
279         
280                 public static char ToChar (byte value) 
281                 { 
282                         return (char)value;
283                 }
284
285                 public static char ToChar (char value) 
286                 { 
287                         return value;
288                 }
289
290                 public static char ToChar (int value) 
291                 { 
292                         if (value > Char.MaxValue || value < Char.MinValue)
293                                 throw new OverflowException
294                                 ("Value is greater than Char.MaxValue or less than Char.MinValue");
295           
296                         return (char)value; 
297                 }
298
299                 public static char ToChar (long value) 
300                 { 
301                         if (value > Char.MaxValue || value < Char.MinValue)
302                                 throw new OverflowException
303                                 ("Value is greater than Char.MaxValue or less than Char.MinValue");
304           
305                         return (char)value; 
306                 }
307
308                 public static char ToChar (sbyte value) 
309                 { 
310                         if (value < Char.MinValue)
311                                 throw new OverflowException
312                                 ("Value is less than Char.MinValue");
313           
314                         return (char)value; 
315                 }
316         
317                 public static char ToChar (short value) 
318                 { 
319                         if (value < Char.MinValue)
320                                 throw new OverflowException
321                                 ("Value is less than Char.MinValue");
322           
323                         return (char)value; 
324                 }
325
326                 public static char ToChar (string value) 
327                 {
328                         return Char.Parse (value);
329                 }
330         
331                 public static char ToChar (uint value) 
332                 { 
333                         if (value > Char.MaxValue)
334                                 throw new OverflowException
335                                 ("Value is greater than Char.MaxValue");
336           
337                         return (char)value; 
338                 }
339
340                 public static char ToChar (ulong value) 
341                 { 
342                         if (value > Char.MaxValue)
343                                 throw new OverflowException
344                                 ("Value is greater than Char.MaxValue");
345           
346                         return (char)value; 
347                 }
348
349                 public static char ToChar (ushort value) 
350                 { 
351                         if (value > Char.MaxValue)
352                                 throw new OverflowException
353                                 ("Value is greater than Char.MaxValue");
354           
355                         return (char)value; 
356                 }
357
358                 // ========== DateTime Conversions ========== //
359         
360                 public static DateTime ToDateTime (string value) 
361                 { 
362                         return DateTime.Parse (value);
363                 }
364         
365                 public static DateTime ToDateTime (string value, IFormatProvider provider) 
366                 {
367                         return DateTime.Parse (value, provider);
368                 }
369
370                 // ========== Decimal Conversions ========== //
371         
372                 public static decimal ToDecimal (bool value) 
373                 { 
374                         return value ? 1 : 0; 
375                 }
376         
377                 public static decimal ToDecimal (byte value) 
378                 { 
379                         return (decimal)value; 
380                 }
381         
382                 public static decimal ToDecimal (decimal value) 
383                 { 
384                         return value; 
385                 }
386
387                 public static decimal ToDecimal (double value) 
388                 { 
389                         if (value > (double)Decimal.MaxValue || value < (double)Decimal.MinValue) 
390                                 throw new OverflowException
391                                 ("Value is greater than Decimal.MaxValue or less than Decimal.MinValue");
392
393                         return (decimal)value; 
394                 }
395
396                 public static decimal ToDecimal (float value) 
397                 { 
398                         if (value > (double)Decimal.MaxValue || value < (double)Decimal.MinValue) 
399                                 throw new OverflowException
400                                 ("Value is greater than Decimal.MaxValue or less than Decimal.MinValue");
401
402                         return (decimal)value; 
403                 }
404
405                 public static decimal ToDecimal (int value) 
406                 { 
407                         return (decimal)value; 
408                 }
409         
410                 public static decimal ToDecimal (long value) 
411                 { 
412                         return (decimal)value; 
413                 }
414
415                 public static decimal ToDecimal (sbyte value) 
416                 { 
417                         return (decimal)value; 
418                 }
419         
420                 public static decimal ToDecimal (short value) 
421                 { 
422                         return (decimal)value; 
423                 }
424
425                 public static decimal ToDecimal (string value) 
426                 {
427                         return Decimal.Parse (value);
428                 }
429
430                 public static decimal ToDecimal (string value, IFormatProvider provider) 
431                 {
432                         return Decimal.Parse (value, provider);
433                 }
434         
435                 public static decimal ToDecimal (uint value) 
436                 { 
437                         return (decimal)value; 
438                 }
439
440                 public static decimal ToDecimal (ulong value) 
441                 { 
442                         return (decimal)value; 
443                 }
444
445                 public static decimal ToDecimal (ushort value) 
446                 { 
447                         return (decimal)value; 
448                 }
449
450                 // ========== Double Conversions ========== //
451         
452                 public static double ToDouble (bool value) 
453                 { 
454                         return value ? 1 : 0; 
455                 }
456         
457                 public static double ToDouble (byte value) 
458                 { 
459                         return (double)value; 
460                 }
461         
462                 public static double ToDouble (decimal value) 
463                 { 
464                         return (double)value; 
465                 }
466
467                 public static double ToDouble (double value) 
468                 { 
469                         return value; 
470                 }
471
472                 public static double ToDouble (float value) 
473                 { 
474                         return (double)value; 
475                 }
476
477                 public static double ToDouble (int value) 
478                 { 
479                         return (double)value; 
480                 }
481         
482                 public static double ToDouble (long value) 
483                 { 
484                         return (double)value; 
485                 }
486
487                 public static double ToDouble (sbyte value) 
488                 { 
489                         return (double)value; 
490                 }
491         
492                 public static double ToDouble (short value) 
493                 { 
494                         return (double)value; 
495                 }
496
497                 public static double ToDouble (string value) 
498                 {
499                         return Double.Parse (value);
500                 }
501
502                 public static double ToDouble (string value, IFormatProvider provider) 
503                 {
504                         return Double.Parse (value, provider);
505                 }
506         
507                 public static double ToDouble (uint value) 
508                 { 
509                         return (double)value; 
510                 }
511
512                 public static double ToDouble (ulong value) 
513                 { 
514                         return (double)value; 
515                 }
516
517                 public static double ToDouble (ushort value) 
518                 { 
519                         return (double)value; 
520                 }
521
522                 // ========== Int16 Conversions ========== //
523
524                 public static short ToInt16 (bool value) 
525                 { 
526                         return (short)(value ? 1 : 0); 
527                 }
528         
529                 public static short ToInt16 (byte value) 
530                 { 
531                         return (short)value; 
532                 }
533
534                 public static short ToInt16 (char value) 
535                 { 
536                         if (value > Int16.MaxValue) 
537                                 throw new OverflowException
538                                 ("Value is greater than Int16.MaxValue");
539
540                         return (short)value; 
541                 }
542         
543                 public static short ToInt16 (decimal value) 
544                 { 
545                         if (value > Int16.MaxValue || value < Int16.MinValue) 
546                                 throw new OverflowException
547                                 ("Value is greater than Int16.MaxValue or less than Int16.MinValue");
548           
549                         // Returned Even-Rounded
550                         return (short)(Math.Round (value));       
551                 }
552
553                 public static short ToInt16 (double value) 
554                 { 
555                         if (value > Int16.MaxValue || value < Int16.MinValue) 
556                                 throw new OverflowException
557                                 ("Value is greater than Int16.MaxValue or less than Int16.MinValue");
558           
559                         // Returned Even-Rounded
560                         return (short)(Math.Round (value));       
561                 }
562  
563                 public static short ToInt16 (float value) 
564                 { 
565                         if (value > Int16.MaxValue || value < Int16.MinValue) 
566                                 throw new OverflowException
567                                 ("Value is greater than Int16.MaxValue or less than Int16.MinValue");
568           
569                         // Returned Even-Rounded, use Math.Round pass as a double.
570                         return (short)Math.Round ( (double)value);
571                 }
572
573                 public static short ToInt16 (int value) 
574                 { 
575                         if (value > Int16.MaxValue || value < Int16.MinValue) 
576                                 throw new OverflowException
577                                 ("Value is greater than Int16.MaxValue or less than Int16.MinValue");
578
579                         return (short)value; 
580                 }
581         
582                 public static short ToInt16 (long value) 
583                 { 
584                         if (value > Int16.MaxValue || value < Int16.MinValue) 
585                                 throw new OverflowException
586                                 ("Value is greater than Int16.MaxValue or less than Int16.MinValue");
587
588                         return (short)value; 
589                 }
590
591                 public static short ToInt16 (sbyte value) 
592                 { 
593                         return (short)value; 
594                 }
595         
596                 public static short ToInt16 (short value) 
597                 { 
598                         return value; 
599                 }
600
601                 public static short ToInt16 (string value) 
602                 {
603                         return Int16.Parse (value);
604                 }
605
606                 public static short ToInt16 (string value, IFormatProvider provider) 
607                 {
608                         return Int16.Parse (value, provider);
609                 }
610         
611                 public static short ToInt16 (uint value) 
612                 { 
613                         if (value > Int16.MaxValue) 
614                                 throw new OverflowException
615                                 ("Value is greater than Int16.MaxValue");
616
617                         return (short)value; 
618                 }
619
620                 public static short ToInt16 (ulong value) 
621                 { 
622                         if (value > (ulong)Int16.MaxValue) 
623                                 throw new OverflowException
624                                 ("Value is greater than Int16.MaxValue");
625
626                         return (short)value; 
627                 }
628
629                 public static short ToInt16 (ushort value) 
630                 { 
631                         if (value > Int16.MaxValue) 
632                                 throw new OverflowException
633                                 ("Value is greater than Int16.MaxValue");
634
635                         return (short)value; 
636                 }
637
638                 // ========== Int32 Conversions ========== //
639
640                 public static int ToInt32 (bool value) 
641                 { 
642                         return value ? 1 : 0; 
643                 }
644         
645                 public static int ToInt32 (byte value) 
646                 { 
647                         return (int)value; 
648                 }
649
650                 public static int ToInt32 (char value) 
651                 { 
652                         return (int)value; 
653                 }
654         
655                 public static int ToInt32 (decimal value) 
656                 { 
657                         if (value > Int32.MaxValue || value < Int32.MinValue) 
658                                 throw new OverflowException
659                                 ("Value is greater than Int32.MaxValue or less than Int32.MinValue");
660
661                         // Returned Even-Rounded
662                         return (int)(Math.Round (value));         
663                 }
664
665                 public static int ToInt32 (double value) 
666                 { 
667                         if (value > Int32.MaxValue || value < Int32.MinValue) 
668                                 throw new OverflowException
669                                 ("Value is greater than Int32.MaxValue or less than Int32.MinValue");
670           
671                         // Returned Even-Rounded
672                         return (int)(Math.Round (value));         
673                 }
674  
675                 public static int ToInt32 (float value) 
676                 { 
677                         if (value > Int32.MaxValue || value < Int32.MinValue) 
678                                 throw new OverflowException
679                                 ("Value is greater than Int32.MaxValue or less than Int32.MinValue");
680           
681                         // Returned Even-Rounded, pass as a double, could just call
682                         // Convert.ToInt32 ( (double)value);
683                         return (int)(Math.Round ( (double)value));
684                 }
685
686                 public static int ToInt32 (int value) 
687                 { 
688                         return value; 
689                 }
690         
691                 public static int ToInt32 (long value) 
692                 { 
693                         if (value > Int32.MaxValue || value < Int32.MinValue) 
694                                 throw new OverflowException
695                                 ("Value is greater than Int32.MaxValue or less than Int32.MinValue");
696
697                         return (int)value; 
698                 }
699
700                 public static int ToInt32 (sbyte value) 
701                 { 
702                         return (int)value; 
703                 }
704         
705                 public static int ToInt32 (short value) 
706                 { 
707                         return (int)value; 
708                 }
709
710                 public static int ToInt32 (string value) 
711                 {
712                         return Int32.Parse (value);
713                 }
714
715                 public static int ToInt32 (string value, IFormatProvider provider) 
716                 {
717                         return Int32.Parse (value, provider);
718                 }
719         
720                 public static int ToInt32 (uint value) 
721                 { 
722                         if (value > Int32.MaxValue) 
723                                 throw new OverflowException
724                                 ("Value is greater than Int32.MaxValue");
725
726                         return (int)value; 
727                 }
728
729                 public static int ToInt32 (ulong value) 
730                 { 
731                         if (value > Int32.MaxValue) 
732                                 throw new OverflowException
733                                 ("Value is greater than Int32.MaxValue");
734
735                         return (int)value; 
736                 }
737
738                 public static int ToInt32 (ushort value) 
739                 { 
740                         return (int)value; 
741                 }
742
743                 // ========== Int64 Conversions ========== //
744
745                 public static long ToInt64 (bool value) 
746                 { 
747                         return value ? 1 : 0; 
748                 }
749         
750                 public static long ToInt64 (byte value) 
751                 { 
752                         return (long)value; 
753                 }
754
755                 public static long ToInt64 (char value) 
756                 { 
757                         return (long)value; 
758                 }
759         
760                 public static long ToInt64 (decimal value) 
761                 { 
762                         if (value > Int64.MaxValue || value < Int64.MinValue) 
763                                 throw new OverflowException
764                                 ("Value is greater than Int64.MaxValue or less than Int64.MinValue");
765           
766                         // Returned Even-Rounded
767                         return (long)(Math.Round (value));        
768                 }
769
770                 public static long ToInt64 (double value) 
771                 { 
772                         if (value > Int64.MaxValue || value < Int64.MinValue) 
773                                 throw new OverflowException
774                                 ("Value is greater than Int64.MaxValue or less than Int64.MinValue");
775           
776                         // Returned Even-Rounded
777                         return (long)(Math.Round (value));        
778                 }
779  
780                 public static long ToInt64 (float value) 
781                 { 
782                         if (value > Int64.MaxValue || value < Int64.MinValue) 
783                                 throw new OverflowException
784                                 ("Value is greater than Int64.MaxValue or less than Int64.MinValue");
785           
786                         // Returned Even-Rounded, pass to Math as a double, could
787                         // just call Convert.ToInt64 ( (double)value);
788                         return (long)(Math.Round ( (double)value));
789                 }
790
791                 public static long ToInt64 (int value) 
792                 { 
793                         return (long)value; 
794                 }
795         
796                 public static long ToInt64 (long value) 
797                 { 
798                         return value; 
799                 }
800
801                 public static long ToInt64 (sbyte value) 
802                 { 
803                         return (long)value; 
804                 }
805         
806                 public static long ToInt64 (short value) 
807                 { 
808                         return (long)value; 
809                 }
810
811                 public static long ToInt64 (string value) 
812                 {
813                         return Int64.Parse (value);
814                 }
815
816                 public static long ToInt64 (string value, IFormatProvider provider) 
817                 {
818                         return Int64.Parse (value, provider);
819                 }
820         
821                 public static long ToInt64 (uint value) 
822                 { 
823                         return (long)value; 
824                 }
825
826                 public static long ToInt64 (ulong value) 
827                 { 
828                         if (value > Int64.MaxValue) 
829                                 throw new OverflowException
830                                 ("Value is greater than Int64.MaxValue");
831
832                         return (long)value; 
833                 }
834
835                 public static long ToInt64 (ushort value) 
836                 { 
837                         return (long)value; 
838                 }
839
840                 // ========== SByte Conversions ========== //
841         
842                 public static sbyte ToSByte (bool value) 
843                 { 
844                         return (sbyte)(value ? 1 : 0); 
845                 }
846         
847                 public static sbyte ToSByte (byte value) 
848                 { 
849                         if (value > SByte.MaxValue)
850                                 throw new OverflowException
851                                 ("Value is greater than SByte.MaxValue");
852
853                         return (sbyte)value; 
854                 }
855
856                 public static sbyte ToSByte (char value) 
857                 { 
858                         if (value > SByte.MaxValue)
859                                 throw new OverflowException
860                                 ("Value is greater than SByte.MaxValue");
861
862                         return (sbyte)value;
863                 }
864         
865                 public static sbyte ToSByte (decimal value) 
866                 { 
867                         if (value > SByte.MaxValue || value < SByte.MinValue)
868                                 throw new OverflowException
869                                 ("Value is greater than SByte.MaxValue or less than SByte.MinValue");
870           
871                         // Returned Even-Rounded
872                         return (sbyte)(Math.Round (value));
873                 }
874         
875                 public static sbyte ToSByte (double value) 
876                 { 
877                         if (value > SByte.MaxValue || value < SByte.MinValue)
878                                 throw new OverflowException
879                                 ("Value is greater than SByte.MaxValue or less than SByte.MinValue");
880
881                         // Returned Even-Rounded
882                         return (sbyte)(Math.Round (value));
883                 }
884
885                 public static sbyte ToSByte (float value) 
886                 { 
887                         if (value > SByte.MaxValue || value < SByte.MinValue)
888                                 throw new OverflowException
889                                 ("Value is greater than SByte.MaxValue or less than SByte.Minalue");
890
891                         // Returned Even-Rounded, pass as double to Math
892                         return (sbyte)(Math.Round ( (double)value));
893                 }
894
895                 public static sbyte ToSByte (int value) 
896                 { 
897                         if (value > SByte.MaxValue || value < SByte.MinValue)
898                                 throw new OverflowException
899                                 ("Value is greater than SByte.MaxValue or less than SByte.MinValue");
900           
901                         return (sbyte)value; 
902                 }
903
904                 public static sbyte ToSByte (long value) 
905                 { 
906                         if (value > SByte.MaxValue || value < SByte.MinValue)
907                                 throw new OverflowException
908                                 ("Value is greater than SByte.MaxValue or less than SByte.MinValue");
909           
910                         return (sbyte)value;
911                 }
912
913                 public static sbyte ToSByte (sbyte value) 
914                 { 
915                         return value;
916                 }
917         
918                 public static sbyte ToSByte (short value) 
919                 { 
920                         if (value > SByte.MaxValue || value < SByte.MinValue)
921                                 throw new OverflowException
922                                 ("Value is greater than SByte.MaxValue or less than SByte.MinValue");
923           
924                         return (sbyte)value; 
925                 }
926
927                 public static sbyte ToSByte (string value) 
928                 {
929                         return SByte.Parse (value);
930                 }
931
932                 public static sbyte ToSByte (string value, IFormatProvider provider) 
933                 {
934                         return SByte.Parse (value, provider);
935                 }
936         
937                 public static sbyte ToSByte (uint value) 
938                 { 
939                         if (value > SByte.MaxValue)
940                                 throw new OverflowException
941                                 ("Value is greater than SByte.MaxValue");
942
943                         return (sbyte)value;
944                 }
945
946                 public static sbyte ToSByte (ulong value) 
947                 { 
948                         if (value > (ulong)SByte.MaxValue)
949                                 throw new OverflowException
950                                 ("Value is greater than SByte.MaxValue");
951
952                         return (sbyte)value;
953                 }
954
955                 public static sbyte ToSByte (ushort value) 
956                 { 
957                         if (value > SByte.MaxValue)
958                                 throw new OverflowException
959                                 ("Value is greater than SByte.MaxValue");
960
961                         return (sbyte)value;
962                 }
963
964                 // ========== Single Conversions ========== //
965         
966                 public static float ToSingle (bool value) 
967                 { 
968                         return value ? 1 : 0; 
969                 }
970         
971                 public static float ToSingle (byte value) 
972                 { 
973                         return (float)value; 
974                 }
975         
976                 public static float ToSingle (decimal value) 
977                 { 
978                         return (float)value; 
979                 }
980
981                 public static float ToSingle (double value) 
982                 { 
983                         if (value > Single.MaxValue || value < Single.MinValue)
984                                 throw new OverflowException
985                                 ("Value is greater than Single.MaxValue or less than Single.MinValue");
986
987                         return (float)value; 
988                 }
989         
990                 public static float ToSingle (float value) 
991                 { 
992                         return value; 
993                 }
994
995                 public static float ToSingle (int value) 
996                 { 
997                         return (float)value; 
998                 }
999         
1000                 public static float ToSingle (long value) 
1001                 { 
1002                         return (float)value; 
1003                 }
1004
1005                 public static float ToSingle (sbyte value) 
1006                 { 
1007                         return (float)value; 
1008                 }
1009         
1010                 public static float ToSingle (short value) 
1011                 { 
1012                         return (float)value; 
1013                 }
1014
1015                 public static float ToSingle (string value) 
1016                 {
1017                         return Single.Parse (value);
1018                 }
1019
1020                 public static float ToSingle (string value, IFormatProvider provider) 
1021                 {
1022                         return Single.Parse (value, provider);
1023                 }
1024         
1025                 public static float ToSingle (uint value) 
1026                 { 
1027                         return (float)value; 
1028                 }
1029
1030                 public static float ToSingle (ulong value) 
1031                 { 
1032                         return (float)value; 
1033                 }
1034
1035                 public static float ToSingle (ushort value) 
1036                 { 
1037                         return (float)value; 
1038                 }
1039
1040                 // ========== String Conversions ========== //
1041         
1042                 public static string ToString (bool value) 
1043                 { 
1044                         return value.ToString (); 
1045                 }
1046         
1047                 public static string ToString (byte value) 
1048                 { 
1049                         return value.ToString (); 
1050                 }
1051         
1052                 public static string ToString (byte value, IFormatProvider provider) 
1053                 {
1054                         return value.ToString (provider); 
1055                 }
1056
1057                 public static string ToString (char value) 
1058                 { 
1059                         return value.ToString (); 
1060                 }
1061
1062                 public static string ToString (DateTime value) 
1063                 { 
1064                         return value.ToString (); 
1065                 }
1066
1067                 public static string ToString (DateTime value, IFormatProvider provider) 
1068                 { 
1069                         return value.ToString (provider); 
1070                 }
1071
1072                 public static string ToString (decimal value) 
1073                 {
1074                         return value.ToString ();
1075                 }
1076
1077                 public static string ToString (decimal value, IFormatProvider provider) 
1078                 { 
1079                         return value.ToString (provider); 
1080                 }
1081         
1082                 public static string ToString (double value) 
1083                 { 
1084                         return value.ToString (); 
1085                 }
1086
1087                 public static string ToString (double value, IFormatProvider provider) 
1088                 { 
1089                         return value.ToString (provider);
1090                 }
1091         
1092                 public static string ToString (float value) 
1093                 { 
1094                         return value.ToString (); 
1095                 }
1096
1097                 public static string ToString (float value, IFormatProvider provider) 
1098                 { 
1099                         return value.ToString (provider); 
1100                 }
1101
1102                 public static string ToString (int value) 
1103                 { 
1104                         return value.ToString (); 
1105                 }
1106
1107                 public static string ToString (int value, IFormatProvider provider) 
1108                 { 
1109                         return value.ToString (provider); 
1110                 }
1111         
1112                 public static string ToString (long value) 
1113                 { 
1114                         return value.ToString (); 
1115                 }
1116
1117                 public static string ToString (long value, IFormatProvider provider) 
1118                 { 
1119                         return value.ToString (provider); 
1120                 }
1121
1122                 public static string ToString (sbyte value) 
1123                 { 
1124                         return value.ToString (); 
1125                 }
1126
1127                 public static string ToString (sbyte value, IFormatProvider provider) 
1128                 { 
1129                         return value.ToString (provider); 
1130                 }
1131         
1132                 public static string ToString (short value) 
1133                 { 
1134                         return value.ToString (); 
1135                 }
1136
1137                 public static string ToString (short value, IFormatProvider provider) 
1138                 { 
1139                         return value.ToString (provider); 
1140                 }
1141
1142                 public static string ToString (string value) 
1143                 {
1144                         return value;
1145                 }
1146
1147                 public static string ToString (uint value) 
1148                 { 
1149                         return value.ToString (); 
1150                 }
1151
1152                 public static string ToString (uint value, IFormatProvider provider) 
1153                 { 
1154                         return value.ToString (provider); 
1155                 }
1156
1157                 public static string ToString (ulong value) 
1158                 { 
1159                         return value.ToString (); 
1160                 }
1161
1162                 public static string ToString (ulong value, IFormatProvider provider) 
1163                 { 
1164                         return value.ToString (provider); 
1165                 }
1166
1167                 public static string ToString (ushort value) 
1168                 { 
1169                         return value.ToString (); 
1170                 }
1171
1172                 public static string ToString (ushort value, IFormatProvider provider) 
1173                 { 
1174                         return value.ToString (provider); 
1175                 }
1176
1177                 // ========== UInt16 Conversions ========== //
1178
1179                 public static ushort ToUInt16 (bool value) 
1180                 { 
1181                         return (ushort)(value ? 1 : 0); 
1182                 }
1183         
1184                 public static ushort ToUInt16 (byte value) 
1185                 { 
1186                         return (ushort)value; 
1187                 }
1188
1189                 public static ushort ToUInt16 (char value) 
1190                 { 
1191                         return (ushort)value; 
1192                 }
1193         
1194                 public static ushort ToUInt16 (decimal value) 
1195                 { 
1196                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
1197                                 throw new OverflowException
1198                                 ("Value is greater than UInt16.MaxValue or less than UInt16.MinValue");
1199           
1200                         // Returned Even-Rounded
1201                         return (ushort)(Math.Round (value));      
1202                 }
1203
1204                 public static ushort ToUInt16 (double value) 
1205                 { 
1206                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
1207                                 throw new OverflowException
1208                                 ("Value is greater than UInt16.MaxValue or less than UInt16.MinValue");
1209           
1210                         // Returned Even-Rounded
1211                         return (ushort)(Math.Round (value));
1212                 }
1213  
1214                 public static ushort ToUInt16 (float value) 
1215                 { 
1216                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
1217                                 throw new OverflowException
1218                                 ("Value is greater than UInt16.MaxValue or less than UInt16.MinValue");
1219           
1220                         // Returned Even-Rounded, pass as double to Math
1221                         return (ushort)(Math.Round ( (double)value));
1222                 }
1223
1224                 public static ushort ToUInt16 (int value) 
1225                 { 
1226                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
1227                                 throw new OverflowException
1228                                 ("Value is greater than UInt16.MaxValue or less than UInt16.MinValue");
1229
1230                         return (ushort)value; 
1231                 }
1232         
1233                 public static ushort ToUInt16 (long value) 
1234                 { 
1235                         if (value > UInt16.MaxValue || value < UInt16.MinValue) 
1236                                 throw new OverflowException
1237                                 ("Value is greater than UInt16.MaxValue or less than UInt16.MinValue");
1238
1239                         return (ushort)value; 
1240                 }
1241
1242                 public static ushort ToUInt16 (sbyte value) 
1243                 { 
1244                         if (value < UInt16.MinValue) 
1245                                 throw new OverflowException
1246                                 ("Value is less than UInt16.MinValue");
1247
1248                         return (ushort)value; 
1249                 }
1250         
1251                 public static ushort ToUInt16 (short value) 
1252                 { 
1253                         if (value < UInt16.MinValue) 
1254                                 throw new OverflowException
1255                                 ("Value is less than UInt16.MinValue");
1256
1257                         return (ushort)value; 
1258                 }
1259
1260                 public static ushort ToUInt16 (string value) 
1261                 {
1262                         return UInt16.Parse (value);
1263                 }
1264
1265                 public static ushort ToUInt16 (string value, IFormatProvider provider) 
1266                 {
1267                         return UInt16.Parse (value, provider);
1268                 }
1269         
1270                 public static ushort ToUInt16 (uint value) 
1271                 { 
1272                         if (value > UInt16.MaxValue) 
1273                                 throw new OverflowException
1274                                 ("Value is greater than UInt16.MaxValue");
1275
1276                         return (ushort)value; 
1277                 }
1278
1279                 public static ushort ToUInt16 (ulong value) 
1280                 { 
1281                         if (value > (ulong)UInt16.MaxValue) 
1282                                 throw new OverflowException
1283                                 ("Value is greater than UInt16.MaxValue");
1284
1285                         return (ushort)value; 
1286                 }
1287
1288                 public static ushort ToUInt16 (ushort value) 
1289                 { 
1290                         return value; 
1291                 }
1292
1293                 // ========== UInt32 Conversions ========== //
1294
1295                 public static uint ToUInt32 (bool value) 
1296                 { 
1297                         return (uint)(value ? 1 : 0); 
1298                 }
1299         
1300                 public static uint ToUInt32 (byte value) 
1301                 { 
1302                         return (uint)value; 
1303                 }
1304
1305                 public static uint ToUInt32 (char value) 
1306                 { 
1307                         return (uint)value; 
1308                 }
1309         
1310                 public static uint ToUInt32 (decimal value) 
1311                 { 
1312                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
1313                                 throw new OverflowException
1314                                 ("Value is greater than UInt32.MaxValue or less than UInt32.MinValue");
1315           
1316                         // Returned Even-Rounded
1317                         return (uint)(Math.Round (value));        
1318                 }
1319
1320                 public static uint ToUInt32 (double value) 
1321                 { 
1322                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
1323                                 throw new OverflowException
1324                                 ("Value is greater than UInt32.MaxValue or less than UInt32.MinValue");
1325           
1326                         // Returned Even-Rounded
1327                         return (uint)(Math.Round (value));        
1328                 }
1329  
1330                 public static uint ToUInt32 (float value) 
1331                 { 
1332                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
1333                                 throw new OverflowException
1334                                 ("Value is greater than UInt32.MaxValue or less than UInt32.MinValue");
1335           
1336                         // Returned Even-Rounded, pass as double to Math
1337                         return (uint)(Math.Round ( (double)value));
1338                 }
1339
1340                 public static uint ToUInt32 (int value) 
1341                 { 
1342                         if (value < UInt32.MinValue) 
1343                                 throw new OverflowException
1344                                 ("Value is less than UInt32.MinValue");
1345
1346                         return (uint)value; 
1347                 }
1348         
1349                 public static uint ToUInt32 (long value) 
1350                 { 
1351                         if (value > UInt32.MaxValue || value < UInt32.MinValue) 
1352                                 throw new OverflowException
1353                                 ("Value is greater than UInt32.MaxValue or less than UInt32.MinValue");
1354
1355                         return (uint)value; 
1356                 }
1357
1358                 public static uint ToUInt32 (sbyte value) 
1359                 { 
1360                         if (value < UInt32.MinValue) 
1361                                 throw new OverflowException
1362                                 ("Value is less than UInt32.MinValue");
1363
1364                         return (uint)value; 
1365                 }
1366         
1367                 public static uint ToUInt32 (short value) 
1368                 { 
1369                         if (value < UInt32.MinValue) 
1370                                 throw new OverflowException
1371                                 ("Value is less than UInt32.MinValue");
1372
1373                         return (uint)value; 
1374                 }
1375
1376                 public static uint ToUInt32 (string value) 
1377                 {
1378                         return UInt32.Parse (value);
1379                 }
1380
1381                 public static uint ToUInt32 (string value, IFormatProvider provider) 
1382                 {
1383                         return UInt32.Parse (value, provider);
1384                 }
1385         
1386                 public static uint ToUInt32 (uint value) 
1387                 { 
1388                         return value; 
1389                 }
1390
1391                 public static uint ToUInt32 (ulong value) 
1392                 { 
1393                         if (value > UInt32.MaxValue) 
1394                                 throw new OverflowException
1395                                 ("Value is greater than UInt32.MaxValue");
1396
1397                         return (uint)value; 
1398                 }
1399
1400                 public static uint ToUInt32 (ushort value) 
1401                 { 
1402                         return (uint)value; 
1403                 }
1404
1405                 // ========== UInt64 Conversions ========== //
1406
1407                 public static ulong ToUInt64 (bool value) 
1408                 { 
1409                         return (ulong)(value ? 1 : 0); 
1410                 }
1411         
1412                 public static ulong ToUInt64 (byte value) 
1413                 { 
1414                         return (ulong)value; 
1415                 }
1416
1417                 public static ulong ToUInt64 (char value) 
1418                 { 
1419                         return (ulong)value; 
1420                 }
1421         
1422                 public static ulong ToUInt64 (decimal value) 
1423                 { 
1424                         if (value > UInt64.MaxValue || value < UInt64.MinValue) 
1425                                 throw new OverflowException
1426                                 ("Value is greater than UInt64.MaxValue or less than UInt64.MinValue");
1427           
1428                         // Returned Even-Rounded
1429                         return (ulong)(Math.Round (value));       
1430                 }
1431
1432                 public static ulong ToUInt64 (double value) 
1433                 { 
1434                         if (value > UInt64.MaxValue || value < UInt64.MinValue) 
1435                                 throw new OverflowException
1436                                 ("Value is greater than UInt64.MaxValue or less than UInt64.MinValue");
1437           
1438                         // Returned Even-Rounded
1439                         return (ulong)(Math.Round (value));       
1440                 }
1441  
1442                 public static ulong ToUInt64 (float value) 
1443                 { 
1444                         if (value > UInt64.MaxValue || value < UInt64.MinValue) 
1445                                 throw new OverflowException
1446                                 ("Value is greater than UInt64.MaxValue or less than UInt64.MinValue");
1447           
1448                         // Returned Even-Rounded, pass as a double to Math
1449                         return (ulong)(Math.Round ( (double)value));
1450                 }
1451
1452                 public static ulong ToUInt64 (int value) 
1453                 { 
1454                         if (value < (int)UInt64.MinValue) 
1455                                 throw new OverflowException
1456                                 ("Value is less than UInt64.MinValue");
1457
1458                         return (ulong)value; 
1459                 }
1460         
1461                 public static ulong ToUInt64 (long value) 
1462                 { 
1463                         if (value < (long)UInt64.MinValue) 
1464                                 throw new OverflowException
1465                                 ("Value is less than UInt64.MinValue");
1466
1467                         return (ulong)value; 
1468                 }
1469
1470                 public static ulong ToUInt64 (sbyte value) 
1471                 { 
1472                         if (value < (sbyte)UInt64.MinValue) 
1473                                 throw new OverflowException
1474                                 ("Value is less than UInt64.MinValue");
1475
1476                         return (ulong)value; 
1477                 }
1478         
1479                 public static ulong ToUInt64 (short value) 
1480                 { 
1481                         if (value < (short)UInt64.MinValue) 
1482                                 throw new OverflowException
1483                                 ("Value is less than UInt64.MinValue");
1484
1485                         return (ulong)value; 
1486                 }
1487
1488                 public static ulong ToUInt64 (string value) 
1489                 {
1490                         return UInt64.Parse (value);
1491                 }
1492
1493                 public static ulong ToUInt64 (string value, IFormatProvider provider) 
1494                 {
1495                         return UInt64.Parse (value, provider);
1496                 }
1497         
1498                 public static ulong ToUInt64 (uint value) 
1499                 { 
1500                         return (ulong)value; 
1501                 }
1502
1503                 public static ulong ToUInt64 (ulong value) 
1504                 { 
1505                         return value; 
1506                 }
1507
1508                 public static ulong ToUInt64 (ushort value) 
1509                 { 
1510                         return (ulong)value; 
1511                 }
1512
1513                 // ========== Conversion / Helper Fucntions ========== //
1514         
1515                 // Lookup table for the conversion ToType method. Order
1516                 // is important! Used by ToType for comparing the target
1517                 // type, and uses hardcoded array indexes.
1518                 private static Type[] conversionTable = {
1519                         // Valid ICovnertible Types
1520                         typeof (Boolean),  //  0 TypeCode.Boolean
1521                         typeof (Byte),     //  1 TypeCode.Byte
1522                         typeof (Char),     //  2 TypeCode.Char
1523                         typeof (DateTime), //  3 TypeCode.DateTime
1524                         typeof (Decimal),  //  4 TypeCode.Decimal
1525                         typeof (Double),   //  5 TypeCode.Double
1526                         typeof (Int16),    //  6 TypeCode.Int16
1527                         typeof (Int32),    //  7 TypeCode.Int32
1528                         typeof (Int64),    //  8 TypeCode.Int64
1529                         typeof (SByte),    //  9 TypeCode.Sbyte
1530                         typeof (Single),   // 10 TypeCode.Single
1531                         typeof (String),   // 11 TypeCode.String
1532                         typeof (UInt16),   // 12 TypeCode.UInt16
1533                         typeof (UInt32),   // 13 TypeCode.UInt32
1534                         typeof (UInt64),   // 14 TypeCode.UInt64
1535           
1536                         // Invalid IConvertible Interface Types
1537                         typeof (Object)    // 15 TypeCode.Object
1538                 };
1539
1540                 // Function to convert an object to another type and return
1541                 // it as an object. In place for the core data types to use
1542                 // when implementing IConvertible. Uses hardcoded indexes in 
1543                 // the conversionTypes array, so if modify carefully.
1544                 internal static object ToType (object value, Type conversionType, 
1545                                                IFormatProvider provider) 
1546                 {
1547                         if (value == null)
1548                                 throw new ArgumentException
1549                                 ("Invalid conversion from null value");
1550
1551                         if (value is IConvertible) {
1552                                 IConvertible convertValue = (IConvertible)value;
1553
1554                                 if (conversionType == conversionTable[0]) {
1555                                         // 0 TypeCode.Boolean
1556                                         return (object)(convertValue.ToBoolean (provider));
1557
1558                                 } else if (conversionType == conversionTable[1]) {
1559                                         // 1 TypeCode.Byte
1560                                         return (object)(convertValue.ToByte (provider));
1561                   
1562                                 } else if (conversionType == conversionTable[2]) {
1563                                         // 2 TypeCode.Char
1564                                         return (object)(convertValue.ToChar (provider));
1565                   
1566                                 } else if (conversionType == conversionTable[3]) {
1567                                         // 3 TypeCode.DateTime
1568                                         return (object)(convertValue.ToDateTime (provider));
1569                   
1570                                 } else if (conversionType == conversionTable[4]) {
1571                                         // 4 TypeCode.Decimal
1572                                         return (object)(convertValue.ToDecimal (provider));
1573                   
1574                                 } else if (conversionType == conversionTable[5]) {
1575                                         // 5 TypeCode.Double
1576                                         return (object)(convertValue.ToDouble (provider));
1577
1578                                 } else if (conversionType == conversionTable[6]) {
1579                                         // 6 TypeCode.Int16
1580                                         return (object)(convertValue.ToInt16 (provider));
1581                   
1582                                 } else if (conversionType == conversionTable[7]) {
1583                                         // 7 TypeCode.Int32
1584                                         return (object)(convertValue.ToInt32 (provider));
1585                   
1586                                 } else if (conversionType == conversionTable[8]) {
1587                                         // 8 TypeCode.Int64
1588                                         return (object)(convertValue.ToInt64 (provider));
1589                   
1590                                 } else if (conversionType == conversionTable[9]) {
1591                                         // 9 TypeCode.Sbyte
1592                                         return (object)(convertValue.ToSByte (provider));
1593                   
1594                                 } else if (conversionType == conversionTable[10]) {
1595                                         // 10 TypeCode.Single
1596                                         return (object)(convertValue.ToSingle (provider));
1597                   
1598                                 } else if (conversionType == conversionTable[11]) {
1599                                         // 11 TypeCode.String
1600                                         return (object)(convertValue.ToString (provider));
1601                   
1602                                 } else if (conversionType == conversionTable[12]) {  
1603                                         // 12 TypeCode.UInt16
1604                                         return (object)(convertValue.ToUInt16 (provider));
1605                   
1606                                 } else if (conversionType == conversionTable[13]) {
1607                                         // 13 TypeCode.UInt32
1608                                         return (object)(convertValue.ToUInt32 (provider));
1609                   
1610                                 } else if (conversionType == conversionTable[14]) {
1611                                         // 14 TypeCode.UInt64
1612                                         return (object)(convertValue.ToUInt64 (provider));
1613
1614                                 } else if (conversionType == conversionTable[15]) {
1615                                         // 15 TypeCode.Object
1616                                         return (object)(value);
1617
1618                                 } else  {               
1619                                         // Not in the conversion table
1620                                         throw new InvalidCastException
1621                                         ("Unknown target conversion type");
1622                                 }
1623                         } else {
1624                                 // Value is not IConvertible
1625                                 throw new ArgumentException
1626                                 ("Value is not a convertible object");
1627                         }
1628                 }
1629         }
1630 }