|
14 | 14 | */
|
15 | 15 |
|
16 | 16 | using System;
|
17 |
| -using MongoDB.Bson.Serialization.Attributes; |
18 | 17 |
|
19 | 18 | namespace MongoDB.Bson.Serialization.Options
|
20 | 19 | {
|
@@ -57,6 +56,70 @@ public bool AllowTruncation
|
57 | 56 | }
|
58 | 57 |
|
59 | 58 | // public methods
|
| 59 | + /// <summary> |
| 60 | + /// Converts an Int32 to a byte. |
| 61 | + /// </summary> |
| 62 | + /// <param name="value">An Int32.</param> |
| 63 | + /// <returns>A byte.</returns> |
| 64 | + public byte ToByte(int value) |
| 65 | + { |
| 66 | + if (value < byte.MinValue || value > byte.MaxValue) |
| 67 | + { |
| 68 | + if (!_allowOverflow) { throw new OverflowException(); } |
| 69 | + return unchecked((byte)value); |
| 70 | + } |
| 71 | + |
| 72 | + return checked((byte)value); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Converts an Int64 to a byte. |
| 77 | + /// </summary> |
| 78 | + /// <param name="value">An Int64.</param> |
| 79 | + /// <returns>A byte.</returns> |
| 80 | + public byte ToByte(long value) |
| 81 | + { |
| 82 | + if (value < byte.MinValue || value > byte.MaxValue) |
| 83 | + { |
| 84 | + if (!_allowOverflow) { throw new OverflowException(); } |
| 85 | + return unchecked((byte)value); |
| 86 | + } |
| 87 | + |
| 88 | + return checked((byte)value); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Converts an Int32 to a char. |
| 93 | + /// </summary> |
| 94 | + /// <param name="value">An Int32.</param> |
| 95 | + /// <returns>A char.</returns> |
| 96 | + public char ToChar(int value) |
| 97 | + { |
| 98 | + if (value < char.MinValue || value > char.MaxValue) |
| 99 | + { |
| 100 | + if (!_allowOverflow) { throw new OverflowException(); } |
| 101 | + return unchecked((char)value); |
| 102 | + } |
| 103 | + |
| 104 | + return checked((char)value); |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Converts an Int64 to a char. |
| 109 | + /// </summary> |
| 110 | + /// <param name="value">An Int64.</param> |
| 111 | + /// <returns>A char.</returns> |
| 112 | + public char ToChar(long value) |
| 113 | + { |
| 114 | + if (value < char.MinValue || value > char.MaxValue) |
| 115 | + { |
| 116 | + if (!_allowOverflow) { throw new OverflowException(); } |
| 117 | + return unchecked((char)value); |
| 118 | + } |
| 119 | + |
| 120 | + return checked((char)value); |
| 121 | + } |
| 122 | + |
60 | 123 | /// <summary>
|
61 | 124 | /// Converts a Decimal128 to a Decimal.
|
62 | 125 | /// </summary>
|
@@ -485,6 +548,26 @@ public short ToInt16(long value)
|
485 | 548 | return (short)value;
|
486 | 549 | }
|
487 | 550 |
|
| 551 | + /// <summary> |
| 552 | + /// Converts a byte to an Int32. |
| 553 | + /// </summary> |
| 554 | + /// <param name="value">A byte.</param> |
| 555 | + /// <returns>An Int32.</returns> |
| 556 | + public int ToInt32(byte value) |
| 557 | + { |
| 558 | + return (int)value; |
| 559 | + } |
| 560 | + |
| 561 | + /// <summary> |
| 562 | + /// Converts a char to an Int32. |
| 563 | + /// </summary> |
| 564 | + /// <param name="value">A char.</param> |
| 565 | + /// <returns>An Int32.</returns> |
| 566 | + public int ToInt32(char value) |
| 567 | + { |
| 568 | + return (int)value; |
| 569 | + } |
| 570 | + |
488 | 571 | /// <summary>
|
489 | 572 | /// Converts a Decimal to an Int32.
|
490 | 573 | /// </summary>
|
@@ -598,6 +681,17 @@ public int ToInt32(long value)
|
598 | 681 | return (int)value;
|
599 | 682 | }
|
600 | 683 |
|
| 684 | + /// <summary> |
| 685 | + /// Converts an sbyte to an Int32. |
| 686 | + /// </summary> |
| 687 | + /// <param name="value">An sbyte.</param> |
| 688 | + /// <returns>An Int32.</returns> |
| 689 | + [CLSCompliant(false)] |
| 690 | + public int ToInt32(sbyte value) |
| 691 | + { |
| 692 | + return (int)value; |
| 693 | + } |
| 694 | + |
601 | 695 | /// <summary>
|
602 | 696 | /// Converts an Int16 to an Int32.
|
603 | 697 | /// </summary>
|
@@ -649,6 +743,26 @@ public int ToInt32(ushort value)
|
649 | 743 | return value;
|
650 | 744 | }
|
651 | 745 |
|
| 746 | + /// <summary> |
| 747 | + /// Converts a byte to an Int64. |
| 748 | + /// </summary> |
| 749 | + /// <param name="value">A byte.</param> |
| 750 | + /// <returns>An Int64.</returns> |
| 751 | + public long ToInt64(byte value) |
| 752 | + { |
| 753 | + return (long)value; |
| 754 | + } |
| 755 | + |
| 756 | + /// <summary> |
| 757 | + /// Converts a char to an Int64. |
| 758 | + /// </summary> |
| 759 | + /// <param name="value">A char.</param> |
| 760 | + /// <returns>An Int64.</returns> |
| 761 | + public long ToInt64(char value) |
| 762 | + { |
| 763 | + return (long)value; |
| 764 | + } |
| 765 | + |
652 | 766 | /// <summary>
|
653 | 767 | /// Converts a Decimal to an Int64.
|
654 | 768 | /// </summary>
|
@@ -758,6 +872,17 @@ public long ToInt64(long value)
|
758 | 872 | return value;
|
759 | 873 | }
|
760 | 874 |
|
| 875 | + /// <summary> |
| 876 | + /// Converts an sbyte to an Int64. |
| 877 | + /// </summary> |
| 878 | + /// <param name="value">An sbyte.</param> |
| 879 | + /// <returns>An Int64.</returns> |
| 880 | + [CLSCompliant(false)] |
| 881 | + public long ToInt64(sbyte value) |
| 882 | + { |
| 883 | + return (long)value; |
| 884 | + } |
| 885 | + |
761 | 886 | /// <summary>
|
762 | 887 | /// Converts an Int16 to an Int64.
|
763 | 888 | /// </summary>
|
@@ -805,6 +930,40 @@ public long ToInt64(ushort value)
|
805 | 930 | return value;
|
806 | 931 | }
|
807 | 932 |
|
| 933 | + /// <summary> |
| 934 | + /// Converts an Int32 to an sbyte. |
| 935 | + /// </summary> |
| 936 | + /// <param name="value">An Int32.</param> |
| 937 | + /// <returns>An sbyte.</returns> |
| 938 | + [CLSCompliant(false)] |
| 939 | + public sbyte ToSByte(int value) |
| 940 | + { |
| 941 | + if (value < sbyte.MinValue || value > sbyte.MaxValue) |
| 942 | + { |
| 943 | + if (!_allowOverflow) { throw new OverflowException(); } |
| 944 | + return unchecked((sbyte)value); |
| 945 | + } |
| 946 | + |
| 947 | + return checked((sbyte)value); |
| 948 | + } |
| 949 | + |
| 950 | + /// <summary> |
| 951 | + /// Converts an Int64 to an sbyte. |
| 952 | + /// </summary> |
| 953 | + /// <param name="value">An Int64.</param> |
| 954 | + /// <returns>An sbyte.</returns> |
| 955 | + [CLSCompliant(false)] |
| 956 | + public sbyte ToSByte(long value) |
| 957 | + { |
| 958 | + if (value < sbyte.MinValue || value > sbyte.MaxValue) |
| 959 | + { |
| 960 | + if (!_allowOverflow) { throw new OverflowException(); } |
| 961 | + return unchecked((sbyte)value); |
| 962 | + } |
| 963 | + |
| 964 | + return checked((sbyte)value); |
| 965 | + } |
| 966 | + |
808 | 967 | /// <summary>
|
809 | 968 | /// Converts a Decimal128 to a Single.
|
810 | 969 | /// </summary>
|
|
0 commit comments