-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
convert.cs
2706 lines (2198 loc) · 102 KB
/
convert.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** Class: Convert
**
**
** Purpose: Home for static conversion methods.
**
**
===========================================================*/
using System;
using System.Globalization;
using System.Threading;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Diagnostics.Contracts;
namespace System {
[Flags]
public enum Base64FormattingOptions {
None = 0,
InsertLineBreaks = 1
}
// Returns the type code of this object. An implementation of this method
// must not return TypeCode.Empty (which represents a null reference) or
// TypeCode.Object (which represents an object that doesn't implement the
// IConvertible interface). An implementation of this method should return
// TypeCode.DBNull if the value of this object is a database null. For
// example, a nullable integer type should return TypeCode.DBNull if the
// value of the object is the database null. Otherwise, an implementation
// of this method should return the TypeCode that best describes the
// internal representation of the object.
// The Value class provides conversion and querying methods for values. The
// Value class contains static members only, and it is not possible to create
// instances of the class.
//
// The statically typed conversion methods provided by the Value class are all
// of the form:
//
// public static XXX ToXXX(YYY value)
//
// where XXX is the target type and YYY is the source type. The matrix below
// shows the set of supported conversions. The set of conversions is symmetric
// such that for every ToXXX(YYY) there is also a ToYYY(XXX).
//
// From: To: Bol Chr SBy Byt I16 U16 I32 U32 I64 U64 Sgl Dbl Dec Dat Str
// ----------------------------------------------------------------------
// Boolean x x x x x x x x x x x x x
// Char x x x x x x x x x x
// SByte x x x x x x x x x x x x x x
// Byte x x x x x x x x x x x x x x
// Int16 x x x x x x x x x x x x x x
// UInt16 x x x x x x x x x x x x x x
// Int32 x x x x x x x x x x x x x x
// UInt32 x x x x x x x x x x x x x x
// Int64 x x x x x x x x x x x x x x
// UInt64 x x x x x x x x x x x x x x
// Single x x x x x x x x x x x x x
// Double x x x x x x x x x x x x x
// Decimal x x x x x x x x x x x x x
// DateTime x x
// String x x x x x x x x x x x x x x x
// ----------------------------------------------------------------------
//
// For dynamic conversions, the Value class provides a set of methods of the
// form:
//
// public static XXX ToXXX(object value)
//
// where XXX is the target type (Boolean, Char, SByte, Byte, Int16, UInt16,
// Int32, UInt32, Int64, UInt64, Single, Double, Decimal, DateTime,
// or String). The implementations of these methods all take the form:
//
// public static XXX toXXX(object value) {
// return value == null? XXX.Default: ((IConvertible)value).ToXXX();
// }
//
// The code first checks if the given value is a null reference (which is the
// same as Value.Empty), in which case it returns the default value for type
// XXX. Otherwise, a cast to IConvertible is performed, and the appropriate ToXXX()
// method is invoked on the object. An InvalidCastException is thrown if the
// cast to IConvertible fails, and that exception is simply allowed to propagate out
// of the conversion method.
// Constant representing the database null value. This value is used in
// database applications to indicate the absense of a known value. Note
// that Value.DBNull is NOT the same as a null object reference, which is
// represented by Value.Empty.
//
// The Equals() method of DBNull always returns false, even when the
// argument is itself DBNull.
//
// When passed Value.DBNull, the Value.GetTypeCode() method returns
// TypeCode.DBNull.
//
// When passed Value.DBNull, the Value.ToXXX() methods all throw an
// InvalidCastException.
public static class Convert {
//A typeof operation is fairly expensive (does a system call), so we'll cache these here
//statically. These are exactly lined up with the TypeCode, eg. ConvertType[TypeCode.Int16]
//will give you the type of an Int16.
internal static readonly RuntimeType[] ConvertTypes = {
(RuntimeType)typeof(System.Empty),
(RuntimeType)typeof(Object),
(RuntimeType)typeof(System.DBNull),
(RuntimeType)typeof(Boolean),
(RuntimeType)typeof(Char),
(RuntimeType)typeof(SByte),
(RuntimeType)typeof(Byte),
(RuntimeType)typeof(Int16),
(RuntimeType)typeof(UInt16),
(RuntimeType)typeof(Int32),
(RuntimeType)typeof(UInt32),
(RuntimeType)typeof(Int64),
(RuntimeType)typeof(UInt64),
(RuntimeType)typeof(Single),
(RuntimeType)typeof(Double),
(RuntimeType)typeof(Decimal),
(RuntimeType)typeof(DateTime),
(RuntimeType)typeof(Object), //TypeCode is discontinuous so we need a placeholder.
(RuntimeType)typeof(String)
};
// Need to special case Enum because typecode will be underlying type, e.g. Int32
private static readonly RuntimeType EnumType = (RuntimeType)typeof(Enum);
internal static readonly char[] base64Table = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
'P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d',
'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
't','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
'8','9','+','/','=' };
private const Int32 base64LineBreakPosition = 76;
#if _DEBUG
private static bool TriggerAsserts = DoAsserts();
private static bool DoAsserts() {
Contract.Assert(ConvertTypes!=null, "[Convert.cctor]ConvertTypes!=null");
Contract.Assert(ConvertTypes.Length == ((int)TypeCode.String + 1), "[Convert.cctor]ConvertTypes.Length == ((int)TypeCode.String + 1)");
Contract.Assert(ConvertTypes[(int)TypeCode.Empty]==typeof(System.Empty),
"[Convert.cctor]ConvertTypes[(int)TypeCode.Empty]==typeof(System.Empty)");
Contract.Assert(ConvertTypes[(int)TypeCode.String]==typeof(String),
"[Convert.cctor]ConvertTypes[(int)TypeCode.String]==typeof(System.String)");
Contract.Assert(ConvertTypes[(int)TypeCode.Int32]==typeof(int),
"[Convert.cctor]ConvertTypes[(int)TypeCode.Int32]==typeof(int)");
return true;
}
#endif
public static readonly Object DBNull = System.DBNull.Value;
// Returns the type code for the given object. If the argument is null,
// the result is TypeCode.Empty. If the argument is not a value (i.e. if
// the object does not implement IConvertible), the result is TypeCode.Object.
// Otherwise, the result is the type code of the object, as determined by
// the object's implementation of IConvertible.
[Pure]
public static TypeCode GetTypeCode(object value) {
if (value == null) return TypeCode.Empty;
IConvertible temp = value as IConvertible;
if (temp != null)
{
return temp.GetTypeCode();
}
return TypeCode.Object;
}
// Returns true if the given object is a database null. This operation
// corresponds to "value.GetTypeCode() == TypeCode.DBNull".
[Pure]
public static bool IsDBNull(object value) {
if (value == System.DBNull.Value) return true;
IConvertible convertible = value as IConvertible;
return convertible != null? convertible.GetTypeCode() == TypeCode.DBNull: false;
}
// Converts the given object to the given type. In general, this method is
// equivalent to calling the Value.ToXXX(value) method for the given
// typeCode and boxing the result.
//
// The method first checks if the given object implements IConvertible. If not,
// the only permitted conversion is from a null to TypeCode.Empty, the
// result of which is null.
//
// If the object does implement IConvertible, a check is made to see if the
// object already has the given type code, in which case the object is
// simply returned. Otherwise, the appropriate ToXXX() is invoked on the
// object's implementation of IConvertible.
public static Object ChangeType(Object value, TypeCode typeCode) {
return ChangeType(value, typeCode, Thread.CurrentThread.CurrentCulture);
}
public static Object ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) {
if (value == null && (typeCode == TypeCode.Empty || typeCode == TypeCode.String || typeCode == TypeCode.Object)) {
return null;
}
IConvertible v = value as IConvertible;
if (v == null) {
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_IConvertible"));
}
// This line is invalid for things like Enums that return a TypeCode
// of Int32, but the object can't actually be cast to an Int32.
// if (v.GetTypeCode() == typeCode) return value;
switch (typeCode) {
case TypeCode.Boolean:
return v.ToBoolean(provider);
case TypeCode.Char:
return v.ToChar(provider);
case TypeCode.SByte:
return v.ToSByte(provider);
case TypeCode.Byte:
return v.ToByte(provider);
case TypeCode.Int16:
return v.ToInt16(provider);
case TypeCode.UInt16:
return v.ToUInt16(provider);
case TypeCode.Int32:
return v.ToInt32(provider);
case TypeCode.UInt32:
return v.ToUInt32(provider);
case TypeCode.Int64:
return v.ToInt64(provider);
case TypeCode.UInt64:
return v.ToUInt64(provider);
case TypeCode.Single:
return v.ToSingle(provider);
case TypeCode.Double:
return v.ToDouble(provider);
case TypeCode.Decimal:
return v.ToDecimal(provider);
case TypeCode.DateTime:
return v.ToDateTime(provider);
case TypeCode.String:
return v.ToString(provider);
case TypeCode.Object:
return value;
case TypeCode.DBNull:
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
case TypeCode.Empty:
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
default:
throw new ArgumentException(Environment.GetResourceString("Arg_UnknownTypeCode"));
}
}
internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) {
Contract.Requires(value != null, "[Convert.DefaultToType]value!=null");
if (targetType==null) {
throw new ArgumentNullException("targetType");
}
Contract.EndContractBlock();
RuntimeType rtTargetType = targetType as RuntimeType;
if (rtTargetType != null)
{
if (value.GetType() == targetType)
{
return value;
}
if (rtTargetType == ConvertTypes[(int)TypeCode.Boolean])
return value.ToBoolean(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Char])
return value.ToChar(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.SByte])
return value.ToSByte(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Byte])
return value.ToByte(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Int16])
return value.ToInt16(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.UInt16])
return value.ToUInt16(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Int32])
return value.ToInt32(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.UInt32])
return value.ToUInt32(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Int64])
return value.ToInt64(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.UInt64])
return value.ToUInt64(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Single])
return value.ToSingle(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Double])
return value.ToDouble(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Decimal])
return value.ToDecimal(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.DateTime])
return value.ToDateTime(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.String])
return value.ToString(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Object])
return (Object)value;
// Need to special case Enum because typecode will be underlying type, e.g. Int32
if (rtTargetType == EnumType)
return (Enum)value;
if (rtTargetType == ConvertTypes[(int)TypeCode.DBNull])
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
if (rtTargetType == ConvertTypes[(int)TypeCode.Empty])
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
}
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", value.GetType().FullName, targetType.FullName));
}
public static Object ChangeType(Object value, Type conversionType) {
return ChangeType(value, conversionType, Thread.CurrentThread.CurrentCulture);
}
public static Object ChangeType(Object value, Type conversionType, IFormatProvider provider) {
if( conversionType == null) {
throw new ArgumentNullException("conversionType");
}
Contract.EndContractBlock();
if( value == null ) {
if(conversionType.IsValueType) {
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCastNullToValueType"));
}
return null;
}
IConvertible ic = value as IConvertible;
if (ic == null) {
if ( value.GetType() == conversionType) {
return value;
}
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_IConvertible"));
}
RuntimeType rtConversionType = conversionType as RuntimeType;
if (rtConversionType==ConvertTypes[(int)TypeCode.Boolean])
return ic.ToBoolean(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.Char])
return ic.ToChar(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.SByte])
return ic.ToSByte(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.Byte])
return ic.ToByte(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.Int16])
return ic.ToInt16(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.UInt16])
return ic.ToUInt16(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.Int32])
return ic.ToInt32(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.UInt32])
return ic.ToUInt32(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.Int64])
return ic.ToInt64(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.UInt64])
return ic.ToUInt64(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.Single])
return ic.ToSingle(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.Double])
return ic.ToDouble(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.Decimal])
return ic.ToDecimal(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.DateTime])
return ic.ToDateTime(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.String])
return ic.ToString(provider);
if (rtConversionType==ConvertTypes[(int)TypeCode.Object])
return (Object)value;
return ic.ToType(conversionType, provider);
}
// Conversions to Boolean
public static bool ToBoolean(Object value) {
return value == null? false: ((IConvertible)value).ToBoolean(null);
}
public static bool ToBoolean(Object value, IFormatProvider provider) {
return value == null? false: ((IConvertible)value).ToBoolean(provider);
}
public static bool ToBoolean(bool value) {
return value;
}
[CLSCompliant(false)]
public static bool ToBoolean(sbyte value) {
return value != 0;
}
// To be consistent with IConvertible in the base data types else we get different semantics
// with widening operations. Without this operator this widen succeeds,with this API the widening throws.
public static bool ToBoolean(char value) {
return ((IConvertible)value).ToBoolean(null);
}
public static bool ToBoolean(byte value) {
return value != 0;
}
public static bool ToBoolean(short value) {
return value != 0;
}
[CLSCompliant(false)]
public static bool ToBoolean(ushort value) {
return value != 0;
}
public static bool ToBoolean(int value) {
return value != 0;
}
[CLSCompliant(false)]
public static bool ToBoolean(uint value) {
return value != 0;
}
public static bool ToBoolean(long value) {
return value != 0;
}
[CLSCompliant(false)]
public static bool ToBoolean(ulong value) {
return value != 0;
}
public static bool ToBoolean(String value) {
if (value == null)
return false;
return Boolean.Parse(value);
}
public static bool ToBoolean(String value, IFormatProvider provider) {
if (value == null)
return false;
return Boolean.Parse(value);
}
public static bool ToBoolean(float value)
{
return value != 0;
}
public static bool ToBoolean(double value)
{
return value != 0;
}
public static bool ToBoolean(decimal value)
{
return value != 0;
}
public static bool ToBoolean(DateTime value)
{
return ((IConvertible)value).ToBoolean(null);
}
// Disallowed conversions to Boolean
// public static bool ToBoolean(TimeSpan value)
// Conversions to Char
public static char ToChar(object value) {
return value == null? (char)0: ((IConvertible)value).ToChar(null);
}
public static char ToChar(object value, IFormatProvider provider) {
return value == null? (char)0: ((IConvertible)value).ToChar(provider);
}
public static char ToChar(bool value) {
return ((IConvertible)value).ToChar(null);
}
public static char ToChar(char value) {
return value;
}
[CLSCompliant(false)]
public static char ToChar(sbyte value) {
if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_Char"));
Contract.EndContractBlock();
return (char)value;
}
public static char ToChar(byte value) {
return (char)value;
}
public static char ToChar(short value) {
if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_Char"));
Contract.EndContractBlock();
return (char)value;
}
[CLSCompliant(false)]
public static char ToChar(ushort value) {
return (char)value;
}
public static char ToChar(int value) {
if (value < 0 || value > Char.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Char"));
Contract.EndContractBlock();
return (char)value;
}
[CLSCompliant(false)]
public static char ToChar(uint value) {
if (value > Char.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Char"));
Contract.EndContractBlock();
return (char)value;
}
public static char ToChar(long value) {
if (value < 0 || value > Char.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Char"));
Contract.EndContractBlock();
return (char)value;
}
[CLSCompliant(false)]
public static char ToChar(ulong value) {
if (value > Char.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Char"));
Contract.EndContractBlock();
return (char)value;
}
//
// @VariantSwitch
// Remove FormatExceptions;
//
public static char ToChar(String value) {
return ToChar(value, null);
}
public static char ToChar(String value, IFormatProvider provider) {
if (value == null)
throw new ArgumentNullException("value");
Contract.EndContractBlock();
if (value.Length != 1)
throw new FormatException(Environment.GetResourceString(ResId.Format_NeedSingleChar));
return value[0];
}
// To be consistent with IConvertible in the base data types else we get different semantics
// with widening operations. Without this operator this widen succeeds,with this API the widening throws.
public static char ToChar(float value)
{
return ((IConvertible)value).ToChar(null);
}
// To be consistent with IConvertible in the base data types else we get different semantics
// with widening operations. Without this operator this widen succeeds,with this API the widening throws.
public static char ToChar(double value)
{
return ((IConvertible)value).ToChar(null);
}
// To be consistent with IConvertible in the base data types else we get different semantics
// with widening operations. Without this operator this widen succeeds,with this API the widening throws.
public static char ToChar(decimal value)
{
return ((IConvertible)value).ToChar(null);
}
public static char ToChar(DateTime value)
{
return ((IConvertible)value).ToChar(null);
}
// Disallowed conversions to Char
// public static char ToChar(TimeSpan value)
// Conversions to SByte
[CLSCompliant(false)]
public static sbyte ToSByte(object value) {
return value == null? (sbyte)0: ((IConvertible)value).ToSByte(null);
}
[CLSCompliant(false)]
public static sbyte ToSByte(object value, IFormatProvider provider) {
return value == null? (sbyte)0: ((IConvertible)value).ToSByte(provider);
}
[CLSCompliant(false)]
public static sbyte ToSByte(bool value) {
return value? (sbyte)Boolean.True: (sbyte)Boolean.False;
}
[CLSCompliant(false)]
public static sbyte ToSByte(sbyte value) {
return value;
}
[CLSCompliant(false)]
public static sbyte ToSByte(char value) {
if (value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
Contract.EndContractBlock();
return (sbyte)value;
}
[CLSCompliant(false)]
public static sbyte ToSByte(byte value) {
if (value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
Contract.EndContractBlock();
return (sbyte)value;
}
[CLSCompliant(false)]
public static sbyte ToSByte(short value) {
if (value < SByte.MinValue || value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
Contract.EndContractBlock();
return (sbyte)value;
}
[CLSCompliant(false)]
public static sbyte ToSByte(ushort value) {
if (value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
Contract.EndContractBlock();
return (sbyte)value;
}
[CLSCompliant(false)]
public static sbyte ToSByte(int value) {
if (value < SByte.MinValue || value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
Contract.EndContractBlock();
return (sbyte)value;
}
[CLSCompliant(false)]
public static sbyte ToSByte(uint value) {
if (value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
Contract.EndContractBlock();
return (sbyte)value;
}
[CLSCompliant(false)]
public static sbyte ToSByte(long value) {
if (value < SByte.MinValue || value > SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
Contract.EndContractBlock();
return (sbyte)value;
}
[CLSCompliant(false)]
public static sbyte ToSByte(ulong value) {
if (value > (ulong)SByte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_SByte"));
Contract.EndContractBlock();
return (sbyte)value;
}
[CLSCompliant(false)]
public static sbyte ToSByte(float value) {
return ToSByte((double)value);
}
[CLSCompliant(false)]
public static sbyte ToSByte(double value) {
return ToSByte(ToInt32(value));
}
[CLSCompliant(false)]
public static sbyte ToSByte(decimal value) {
return Decimal.ToSByte(Decimal.Round(value, 0));
}
[CLSCompliant(false)]
public static sbyte ToSByte(String value) {
if (value == null)
return 0;
return SByte.Parse(value, CultureInfo.CurrentCulture);
}
[CLSCompliant(false)]
public static sbyte ToSByte(String value, IFormatProvider provider) {
return SByte.Parse(value, NumberStyles.Integer, provider);
}
[CLSCompliant(false)]
public static sbyte ToSByte(DateTime value)
{
return ((IConvertible)value).ToSByte(null);
}
// Disallowed conversions to SByte
// public static sbyte ToSByte(TimeSpan value)
// Conversions to Byte
public static byte ToByte(object value) {
return value == null? (byte)0: ((IConvertible)value).ToByte(null);
}
public static byte ToByte(object value, IFormatProvider provider) {
return value == null? (byte)0: ((IConvertible)value).ToByte(provider);
}
public static byte ToByte(bool value) {
return value? (byte)Boolean.True: (byte)Boolean.False;
}
public static byte ToByte(byte value) {
return value;
}
public static byte ToByte(char value) {
if (value > Byte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
Contract.EndContractBlock();
return (byte)value;
}
[CLSCompliant(false)]
public static byte ToByte(sbyte value) {
if (value < Byte.MinValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
Contract.EndContractBlock();
return (byte)value;
}
public static byte ToByte(short value) {
if (value < Byte.MinValue || value > Byte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
Contract.EndContractBlock();
return (byte)value;
}
[CLSCompliant(false)]
public static byte ToByte(ushort value) {
if (value > Byte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
Contract.EndContractBlock();
return (byte)value;
}
public static byte ToByte(int value) {
if (value < Byte.MinValue || value > Byte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
Contract.EndContractBlock();
return (byte)value;
}
[CLSCompliant(false)]
public static byte ToByte(uint value) {
if (value > Byte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
Contract.EndContractBlock();
return (byte)value;
}
public static byte ToByte(long value) {
if (value < Byte.MinValue || value > Byte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
Contract.EndContractBlock();
return (byte)value;
}
[CLSCompliant(false)]
public static byte ToByte(ulong value) {
if (value > Byte.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));
Contract.EndContractBlock();
return (byte)value;
}
public static byte ToByte(float value) {
return ToByte((double)value);
}
public static byte ToByte(double value) {
return ToByte(ToInt32(value));
}
public static byte ToByte(decimal value) {
return Decimal.ToByte(Decimal.Round(value, 0));
}
public static byte ToByte(String value) {
if (value == null)
return 0;
return Byte.Parse(value, CultureInfo.CurrentCulture);
}
public static byte ToByte(String value, IFormatProvider provider) {
if (value == null)
return 0;
return Byte.Parse(value, NumberStyles.Integer, provider);
}
public static byte ToByte(DateTime value)
{
return ((IConvertible)value).ToByte(null);
}
// Disallowed conversions to Byte
// public static byte ToByte(TimeSpan value)
// Conversions to Int16
public static short ToInt16(object value) {
return value == null? (short)0: ((IConvertible)value).ToInt16(null);
}
public static short ToInt16(object value, IFormatProvider provider) {
return value == null? (short)0: ((IConvertible)value).ToInt16(provider);
}
public static short ToInt16(bool value) {
return value? (short)Boolean.True: (short)Boolean.False;
}
public static short ToInt16(char value) {
if (value > Int16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
Contract.EndContractBlock();
return (short)value;
}
[CLSCompliant(false)]
public static short ToInt16(sbyte value) {
return value;
}
public static short ToInt16(byte value) {
return value;
}
[CLSCompliant(false)]
public static short ToInt16(ushort value) {
if (value > Int16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
Contract.EndContractBlock();
return (short)value;
}
public static short ToInt16(int value) {
if (value < Int16.MinValue || value > Int16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
Contract.EndContractBlock();
return (short)value;
}
[CLSCompliant(false)]
public static short ToInt16(uint value) {
if (value > Int16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
Contract.EndContractBlock();
return (short)value;
}
public static short ToInt16(short value) {
return value;
}
public static short ToInt16(long value) {
if (value < Int16.MinValue || value > Int16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
Contract.EndContractBlock();
return (short)value;
}
[CLSCompliant(false)]
public static short ToInt16(ulong value) {
if (value > (ulong)Int16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
Contract.EndContractBlock();
return (short)value;
}
public static short ToInt16(float value) {
return ToInt16((double)value);
}
public static short ToInt16(double value) {
return ToInt16(ToInt32(value));
}
public static short ToInt16(decimal value) {
return Decimal.ToInt16(Decimal.Round(value, 0));
}
public static short ToInt16(String value) {
if (value == null)
return 0;
return Int16.Parse(value, CultureInfo.CurrentCulture);
}
public static short ToInt16(String value, IFormatProvider provider) {
if (value == null)
return 0;
return Int16.Parse(value, NumberStyles.Integer, provider);
}
public static short ToInt16(DateTime value)
{
return ((IConvertible)value).ToInt16(null);
}
// Disallowed conversions to Int16
// public static short ToInt16(TimeSpan value)
// Conversions to UInt16
[CLSCompliant(false)]
public static ushort ToUInt16(object value) {
return value == null? (ushort)0: ((IConvertible)value).ToUInt16(null);
}
[CLSCompliant(false)]
public static ushort ToUInt16(object value, IFormatProvider provider) {
return value == null? (ushort)0: ((IConvertible)value).ToUInt16(provider);
}
[CLSCompliant(false)]
public static ushort ToUInt16(bool value) {
return value? (ushort)Boolean.True: (ushort)Boolean.False;
}
[CLSCompliant(false)]
public static ushort ToUInt16(char value) {
return value;
}
[CLSCompliant(false)]
public static ushort ToUInt16(sbyte value) {
if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
Contract.EndContractBlock();
return (ushort)value;
}
[CLSCompliant(false)]
public static ushort ToUInt16(byte value) {
return value;
}
[CLSCompliant(false)]
public static ushort ToUInt16(short value) {
if (value < 0) throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
Contract.EndContractBlock();
return (ushort)value;
}
[CLSCompliant(false)]
public static ushort ToUInt16(int value) {
if (value < 0 || value > UInt16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
Contract.EndContractBlock();
return (ushort)value;
}
[CLSCompliant(false)]
public static ushort ToUInt16(ushort value) {
return value;
}
[CLSCompliant(false)]
public static ushort ToUInt16(uint value) {
if (value > UInt16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
Contract.EndContractBlock();
return (ushort)value;
}
[CLSCompliant(false)]
public static ushort ToUInt16(long value) {
if (value < 0 || value > UInt16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
Contract.EndContractBlock();
return (ushort)value;
}
[CLSCompliant(false)]
public static ushort ToUInt16(ulong value) {
if (value > UInt16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
Contract.EndContractBlock();
return (ushort)value;
}
[CLSCompliant(false)]
public static ushort ToUInt16(float value) {
return ToUInt16((double)value);
}
[CLSCompliant(false)]
public static ushort ToUInt16(double value) {
return ToUInt16(ToInt32(value));
}
[CLSCompliant(false)]
public static ushort ToUInt16(decimal value) {
return Decimal.ToUInt16(Decimal.Round(value, 0));
}
[CLSCompliant(false)]
public static ushort ToUInt16(String value) {