private Func <Type, string, ParseStep.Result> _CreateParseContext() { var _stack = new HashSet <ParseStep>(); Func <Type, string, ParseStep.Result> _tryParse = null; _tryParse = (_type, _s) => { var _parseSteps = _GetParseSteps(_type); if (_parseSteps.Length == 0) { return(StringParseResult.UnsupportedType(_type, _s)); } foreach (var _parseStep in _parseSteps) { if (_stack.Add(_parseStep)) { return(_parseStep.TryParse(_type, _s, _CultureInfo, _tryParse)); } } return(StringParseResult.InvalidString(_type, _s)); }; return(_tryParse); }
public override StringParseResult <object> TryParse(Type type, string s) { try { return(StringParseResult.Valid(type, s, Convert.ChangeType(s, type, _CultureInfo))); } catch (InvalidCastException) { return(StringParseResult.UnsupportedType(type, s)); } catch (FormatException) { return(StringParseResult.InvalidString(type, s)); } catch (OverflowException) { return(StringParseResult.InvalidString(type, s)); } }
public StringParseResult <object> ToStringParseResult(Type type, string s) { if (UnderlyingResult.HasValue) { return(UnderlyingResult.Value); } if (IsTypeSupported.GetValueOrDefault() && IsStringValid.GetValueOrDefault()) { return(StringParseResult.Valid(type, s, Value)); } if (!IsTypeSupported.GetValueOrDefault(true)) { return(StringParseResult.UnsupportedType(type, s)); } if (!IsStringValid.GetValueOrDefault(true)) { return(StringParseResult.InvalidString(type, s)); } throw new UnexpectedStringParseErrorException(); }