public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); var modelState = new ModelState { Value = valueResult }; object actualValue = null; if (!string.IsNullOrWhiteSpace(valueResult.AttemptedValue)) { try { var type = bindingContext.ModelType; var underlyingType = Nullable.GetUnderlyingType(type); var valueType = underlyingType ?? type; actualValue = NumericValueParser.Parse(valueResult.AttemptedValue, valueType, valueResult.Culture); } catch (Exception e) { modelState.Errors.Add(e); } } bindingContext.ModelState.Add(bindingContext.ModelName, modelState); return(actualValue); }
public void ParseFromExpressionDouble_NormalScenario() { foreach (var sample in GoodSamples) { var result = NumericValueParser.ParseFromExpressionDouble(sample.Expression); Assert.AreEqual(result, sample.Correct, $"expected {sample.Correct} but {result} | on \"{sample.Expression.ToFullString()}\""); } }
public void ParseFromExpressionDoubleOrNull_IncorrectScenario() { foreach (var expression in BadExpressions) { var result = NumericValueParser.ParseFromExpressionDoubleOrNull(expression); Assert.IsNull(result, $"incorrect result : {result} | parse mistakenly passed on \"{expression.ToFullString()}\""); } }
public void TryParseFromExpression_IncorrectScenario() { foreach (var expression in BadExpressions) { double result; Assert.IsFalse(NumericValueParser.TryParseFromExpression(expression, out result), $"incorrect result : {result} | parse mistakenly passed on \"{expression.ToFullString()}\""); } }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { try { return(NumericValueParser.Parse((string)value, type, culture)); } catch { } } return(baseConverter.ConvertFrom(context, culture, value)); }
public void TryParseFromExpression_NormalScenario() { foreach (var sample in GoodSamples) { double result; Assert.IsTrue(NumericValueParser.TryParseFromExpression(sample.Expression, out result), $"parse error on \"{sample.Expression.ToFullString()}\""); Assert.AreEqual(result, sample.Correct, $"expected {sample.Correct} but {result} | on \"{sample.Expression.ToFullString()}\""); } }
public void ParseFromExpressionDouble_IncorrectScenario() { foreach (var expression in BadExpressions) { try { var result = NumericValueParser.ParseFromExpressionDouble(expression); Assert.Fail($"incorrect result : {result} | parse mistakenly passed on \"{expression.ToFullString()}\""); } catch (FormatException) { } // ok } }
private static IEnumerable <Tuple <InvocationExpressionSyntax, int?> > GetFirstArgConstInt(IEnumerable <InvocationExpressionSyntax> invocations) { return(invocations.Select(g => Tuple.Create(g, NumericValueParser.ParseFromExpressionIntOrNull(g.GetArgumentExpressionAt(0))))); }
private bool TryParseNumber(string s, Type numericType, out object number) { return(NumericValueParser.TryParse(numericType, s, Configuration.NumberStyles, Configuration.FormatProvider, out number)); }