Exemplo n.º 1
0
        private bool EvaluateIsValid <T> (
            string text, NumberStyles numberStyle, TryParseDelegate <T> tryParse, TryParseWithNumberStyleDelegate <T> tryParseWithNumberStyle)
            where T : struct, IComparable <T>
        {
            T parsedValue;

            if (_numberStyle != NumberStyles.None)
            {
                try
                {
                    if (!tryParseWithNumberStyle(text, numberStyle, null, out parsedValue))
                    {
                        return(false);
                    }
                }
                catch (ArgumentException e)
                {
                    throw new InvalidOperationException("The combination of the flags in the 'NumberStyle' property is invalid.", e);
                }
            }
            else if (!tryParse(text, out parsedValue))
            {
                return(false);
            }

            if (!_allowNegative && parsedValue.CompareTo(default(T)) < 0)
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
            static StringConverter()
            {
                var tryParseMethods = typeof(T).GetMethods(BindingFlags.Static | BindingFlags.Public)
                                      .Where(x => x.Name == "TryParse" && x.ReturnType == typeof(bool)).ToArray();

                tryParseWithNumberStyleMethod = RecognizeTryParseWithNumberStyleMethod(tryParseMethods,
                                                                                       out defaultNumberStyle);
                tryParseMethod = RecognizeTryParseWithNoExtraArguments(tryParseMethods);
            }