From 814833852b7c606b865208902b6f4703462179ca Mon Sep 17 00:00:00 2001 From: dahall Date: Tue, 31 Mar 2020 16:00:36 -0600 Subject: [PATCH] Major changes which could affect anticipated behavior: All conversions to ordinals now look for max value and will return converted value's max value. No more invalid conversions. --- Core/InteropServices/SizeT.cs | 110 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 15 deletions(-) diff --git a/Core/InteropServices/SizeT.cs b/Core/InteropServices/SizeT.cs index bca2cf1f..e1e5e068 100644 --- a/Core/InteropServices/SizeT.cs +++ b/Core/InteropServices/SizeT.cs @@ -58,17 +58,17 @@ namespace Vanara.PInvoke /// Performs an implicit conversion from to . /// The value. /// The result of the conversion. - public static implicit operator int(SizeT value) => (int)value.val.ToUInt32(); + public static implicit operator int(SizeT value) => ((IConvertible)value).ToInt32(null); /// Performs an implicit conversion from to . /// The value. /// The result of the conversion. - public static implicit operator uint(SizeT value) => value.val.ToUInt32(); + public static implicit operator uint(SizeT value) => ((IConvertible)value).ToUInt32(null); /// Performs an implicit conversion from to . /// The value. /// The result of the conversion. - public static implicit operator long(SizeT value) => (long)value.Value; + public static implicit operator long(SizeT value) => ((IConvertible)value).ToInt64(null); /// Performs an implicit conversion from to . /// The value. @@ -153,7 +153,15 @@ namespace Vanara.PInvoke bool IConvertible.ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); /// - byte IConvertible.ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + byte IConvertible.ToByte(IFormatProvider provider) + { + var ul = Value; + if (ul < (ulong)byte.MaxValue) + return (byte)ul; + if (ul == uint.MaxValue || ul == ulong.MaxValue) + return byte.MaxValue; + throw new OverflowException(); + } /// char IConvertible.ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); @@ -162,37 +170,109 @@ namespace Vanara.PInvoke DateTime IConvertible.ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); /// - decimal IConvertible.ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + decimal IConvertible.ToDecimal(IFormatProvider provider) + { + var ul = Value; + if (ul < decimal.MaxValue) + return (decimal)ul; + if (ul == uint.MaxValue || ul == ulong.MaxValue) + return decimal.MaxValue; + throw new OverflowException(); + } /// - double IConvertible.ToDouble(IFormatProvider provider) => ((IConvertible)Value).ToDouble(provider); + double IConvertible.ToDouble(IFormatProvider provider) + { + var ul = Value; + if (ul < double.MaxValue) + return ul; + if (ul == uint.MaxValue || ul == ulong.MaxValue) + return double.MaxValue; + throw new OverflowException(); + } /// - short IConvertible.ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + short IConvertible.ToInt16(IFormatProvider provider) + { + var ul = Value; + if (ul < (ulong)short.MaxValue) + return (short)ul; + if (ul == uint.MaxValue || ul == ulong.MaxValue) + return short.MaxValue; + throw new OverflowException(); + } /// - int IConvertible.ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + int IConvertible.ToInt32(IFormatProvider provider) + { + var ul = Value; + if (ul < int.MaxValue) + return (int)ul; + if (ul == uint.MaxValue || ul == ulong.MaxValue) + return int.MaxValue; + throw new OverflowException(); + } /// - long IConvertible.ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + long IConvertible.ToInt64(IFormatProvider provider) + { + var ul = Value; + if (ul < long.MaxValue) + return (long)ul; + if (ul == uint.MaxValue || ul == ulong.MaxValue) + return long.MaxValue; + throw new OverflowException(); + } /// - sbyte IConvertible.ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + sbyte IConvertible.ToSByte(IFormatProvider provider) + { + var ul = Value; + if (ul < (ulong)sbyte.MaxValue) + return (sbyte)ul; + if (ul == uint.MaxValue || ul == ulong.MaxValue) + return sbyte.MaxValue; + throw new OverflowException(); + } /// - float IConvertible.ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + float IConvertible.ToSingle(IFormatProvider provider) + { + var ul = Value; + if (ul < float.MaxValue) + return ul; + if (ul == uint.MaxValue || ul == ulong.MaxValue) + return float.MaxValue; + throw new OverflowException(); + } /// - object IConvertible.ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + object IConvertible.ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToType(conversionType, provider); /// - ushort IConvertible.ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + ushort IConvertible.ToUInt16(IFormatProvider provider) + { + var ul = Value; + if (ul < (ulong)ushort.MaxValue) + return (ushort)ul; + if (ul == uint.MaxValue || ul == ulong.MaxValue) + return ushort.MaxValue; + throw new OverflowException(); + } /// - uint IConvertible.ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + uint IConvertible.ToUInt32(IFormatProvider provider) + { + var ul = Value; + if (ul < uint.MaxValue) + return (uint)ul; + if (ul == uint.MaxValue || ul == ulong.MaxValue) + return uint.MaxValue; + throw new OverflowException(); + } /// - ulong IConvertible.ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + ulong IConvertible.ToUInt64(IFormatProvider provider) => Value; internal class SizeTTypeConverter : UInt64Converter {