Beispiel #1
0
        /// <summary>
        /// Attempts to set the value by either TypeDescriptor.GetConverter or by a custom implementation was passed using UseSetter.  returns bool of indicating success of setting the value.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="err"></param>
        /// <returns></returns>
        public bool TrySetValue(string value, out ParsingError err)
        {
            try
            {
                if (_setter != null)
                {
                    _value    = _setter(_value, value);
                    _hasValue = true;
                }
                else
                {
                    var converter = TypeDescriptor.GetConverter(typeof(T));
                    _value    = (T)converter.ConvertFrom(value);
                    _hasValue = true;
                }
                err = null;
                return(true);
            }
            catch (Exception e)
            {
                if (e?.InnerException is OverflowException)
                {
                    err = new ParsingError("value out of range", e);
                }
                else
                {
                    err = new ParsingError("parse error", e);
                }

                return(false);
            }
        }
Beispiel #2
0
        internal ParsingError FailF(string format, params object[] args)
        {
            var error = new ParsingError(string.Format(format, args));

            Output().Write($"{error}\n");
            Usage();
            return(error);
        }