示例#1
0
        public static bool SetRealValue <TValue>(this string str, out TValue value)
        {
            value = default(TValue);

            var otype = typeof(TValue);
            var type  = Nullable.GetUnderlyingType(otype) ?? otype;

            if (string.IsNullOrEmpty(str) && otype != typeof(string))
            {
                if (otype != type)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            if (type == typeof(bool))
            {
                if (str.Equals("true", StringComparison.OrdinalIgnoreCase))
                {
                    value = (TValue)(object)true;
                    return(true);
                }
                else if (str.Equals("false", StringComparison.OrdinalIgnoreCase))
                {
                    value = (TValue)(object)false;
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            //好奇怪,TryConvertTo<DateTimeOffset> 不行,估计是个bug
            if (type == typeof(DateTimeOffset))
            {
                if (BindConverter.TryConvertToDateTimeOffset(str, CultureInfo.InvariantCulture, out DateTimeOffset val))
                {
                    value = (TValue)(object)val;
                    return(true);
                }

                return(false);
            }
            else
            {
                if (BindConverter.TryConvertTo <TValue>(str, CultureInfo.InvariantCulture, out TValue val))
                {
                    value = val;
                    return(true);
                }

                return(false);
            }
        }
示例#2
0
        static bool TryParseDateTimeOffset(string?value, [MaybeNullWhen(false)] out TValue result)
        {
            var success = BindConverter.TryConvertToDateTimeOffset(value, CultureInfo.InvariantCulture, DateFormat, out var parsedValue);

            if (success)
            {
                result = (TValue)(object)parsedValue;
                return(true);
            }
            else
            {
                result = default;
                return(false);
            }
        }
示例#3
0
        static bool TryParseDateTimeOffset(string value, out TValue result)
        {
            var success = BindConverter.TryConvertToDateTimeOffset(value, cultureInfoStatic, DateFormat, out var parsedValue);

            if (success)
            {
                result = (TValue)(object)parsedValue;
                return(true);
            }
            else
            {
                result = default;
                return(false);
            }
        }
示例#4
0
        private static bool ToDateTimeOffset(string value, out T result)
        {
            var success = BindConverter.TryConvertToDateTimeOffset(value, CultureInfo.InvariantCulture, _dateFormat, out DateTimeOffset parsedValue);

            if (success)
            {
                result = (T)(object)parsedValue;
                return(true);
            }
            else
            {
                result = default;
                return(false);
            }
        }
示例#5
0
        static bool TryParseDateTimeOffset(string value, bool localize, out TValue result)
        {
            var success = BindConverter.TryConvertToDateTimeOffset(value, CultureInfo.InvariantCulture, out var parsedValue);

            if (success)
            {
                if (localize)
                {
                    result = (TValue)(object)parsedValue.ToUniversalTime();
                }
                else
                {
                    result = (TValue)(object)parsedValue;
                }
                return(true);
            }
            else
            {
                result = default;
                return(false);
            }
        }
示例#6
0
        private static bool TryParseDateTimeOffset(string value, out TValue result)
        {
            // Try Culture format
            var success = BindConverter.TryConvertToDateTimeOffset(value, CultureInfo.CurrentUICulture, out DateTimeOffset parsedValue);

            // Success?
            if (!success)
            {
                // Try ISO format
                success = BindConverter.TryConvertToDateTimeOffset(value, CultureInfo.CurrentUICulture, ISO_FORMAT, out parsedValue);
            }

            if (success)
            {
                result = (TValue)(object)parsedValue;
                return(true);
            }
            else
            {
                result = default;
                return(false);
            }
        }
示例#7
0
        public static bool SetRealValue <TValue>(this string str, out TValue value, string format = null)
        {
            value = default(TValue);

            var otype = typeof(TValue);
            var type  = Nullable.GetUnderlyingType(otype) ?? otype;

            if (string.IsNullOrEmpty(str) && otype != typeof(string))
            {
                if (otype != type)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            if (type == typeof(bool))
            {
                if (str.Equals("true", StringComparison.OrdinalIgnoreCase))
                {
                    value = (TValue)(object)true;
                    return(true);
                }
                else if (str.Equals("false", StringComparison.OrdinalIgnoreCase))
                {
                    value = (TValue)(object)false;
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            //好奇怪,TryConvertTo<DateTimeOffset> 不行,估计是个bug
            if (type == typeof(DateTimeOffset))
            {
                if (BindConverter.TryConvertToDateTimeOffset(str, CultureInfo.InvariantCulture, out DateTimeOffset val))
                {
                    value = (TValue)(object)val;
                    return(true);
                }

                return(false);
            }
            else
            {
                if (str.HasValue())
                {
                    if ("P".Equals(format, StringComparison.OrdinalIgnoreCase) ||
                        "P1".Equals(format, StringComparison.OrdinalIgnoreCase))
                    {
                        str = str.Replace("%", "").Trim();
                        str = (decimal.Parse(str) / 100).ToString();
                    }
                    else if ("C".Equals(format, StringComparison.OrdinalIgnoreCase))
                    {
                        if (!Char.IsDigit(str[0]))
                        {
                            str = str.Remove(0, 1);
                        }
                    }
                }

                if (BindConverter.TryConvertTo <TValue>(str, CultureInfo.InvariantCulture, out TValue val))
                {
                    value = val;
                    return(true);
                }

                return(false);
            }
        }