/// <summary>
        /// Initializes and creates one <see cref="NumberStringConverter{T}"/>
        /// instance for each primitive numeric data type using the specified
        /// <see cref="IFormatProvider"/>.
        /// </summary>
        /// <param name="formatProvider">
        /// An <see cref="IFormatProvider"/> that supplies culture-specific
        /// formatting information for conversion. Omit or specify <see langword="null"/>
        /// to use the <see cref="NumberFormatInfo"/> for the current culture.
        /// </param>
        public PrimitiveNumberStringConverter(IFormatProvider?formatProvider = default)
        {
            Int8  = new NumberStringConverter <sbyte>(sbyte.Parse, sbyte.TryParse, formatProvider);
            UInt8 = new NumberStringConverter <byte>(byte.Parse, byte.TryParse, formatProvider);

            Int16  = new NumberStringConverter <short>(short.Parse, short.TryParse, formatProvider);
            UInt16 = new NumberStringConverter <ushort>(ushort.Parse, ushort.TryParse, formatProvider);

            Int32  = new NumberStringConverter <int>(int.Parse, int.TryParse, formatProvider);
            UInt32 = new NumberStringConverter <uint>(uint.Parse, uint.TryParse, formatProvider);

            Int64  = new NumberStringConverter <long>(long.Parse, long.TryParse, formatProvider);
            UInt64 = new NumberStringConverter <ulong>(ulong.Parse, ulong.TryParse, formatProvider);

            Single  = new NumberStringConverter <float>(float.Parse, float.TryParse, formatProvider);
            Double  = new NumberStringConverter <double>(double.Parse, double.TryParse, formatProvider);
            Decimal = new NumberStringConverter <decimal>(decimal.Parse, decimal.TryParse, formatProvider);
        }
Ejemplo n.º 2
0
            static EnumTryParseFunc <TEnum> InitializeNumericTryParseFromUnderlyingType <TNumeric, TEnum>(NumberStringConverter <TNumeric> numberStringConverter)
                where TNumeric : unmanaged, IFormattable
                where TEnum : unmanaged, Enum
            {
                return(TryParse);

                bool TryParse(string s, out TEnum value)
                {
                    if (numberStringConverter.TryParse(s, out var numeric))
                    {
                        unsafe
                        {
                            TEnum *pNumeric = (TEnum *)&numeric;
                            value = *pNumeric;
                        }
                    }

                    value = default;
                    return(false);
                }
            }