public bool TryParse(string input, out Range <T> result) { result = null; if (string.IsNullOrWhiteSpace(input)) { return(false); } Match match = regex.Match(input); if (!match.Success) { return(false); } bool includeMinValue = match.Groups[IncludeMinValueGroupName].Value == "["; bool includeMaxValue = match.Groups[IncludeMaxValueGroupName].Value == "]"; string minValueCapture = match.Groups[MinValueGroupName].Value; string maxValueCapture = match.Groups[MaxValueGroupName].Value; T minValue, maxValue; if (!valueParser.TryParse(minValueCapture, out minValue) || !valueParser.TryParse(maxValueCapture, out maxValue)) { return(false); } result = new Range <T>(minValue, maxValue, includeMinValue, includeMaxValue); return(true); }
public bool TryParse(string input, out List <T> result) { result = null; if (string.IsNullOrWhiteSpace(input)) { return(false); } var list = new List <T>(); var values = input.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (var value in values) { if (string.IsNullOrWhiteSpace(value)) { continue; } T element; if (!valueParser.TryParse(value.Trim(), out element)) { return(false); } list.Add(element); } if (list.Count == 0) { return(false); } result = list; return(true); }
public bool TryParse(string input, out T?result) { T value; bool success = valueParser.TryParse(input, out value); result = success ? value : default(T); return(success); }
public bool TryParse(string input, out Dictionary <TKey, TValue> result) { result = null; if (string.IsNullOrWhiteSpace(input)) { return(false); } var dictionary = new Dictionary <TKey, TValue>(); var keyValuePairs = input.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (var keyValuePair in keyValuePairs) { if (string.IsNullOrWhiteSpace(keyValuePair)) { continue; } var parts = keyValuePair.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 2) { return(false); } string keyString = parts[0].Trim(); string valueString = parts[1].Trim(); TKey key; TValue value; if (!keyParser.TryParse(keyString, out key) || !valueParser.TryParse(valueString, out value)) { return(false); } dictionary[key] = value; } if (dictionary.Count == 0) { return(false); } result = dictionary; return(true); }
private Expression GetFilterExpression <TEntity>(Filter filter, Expression entityParam) where TEntity : class, new() { PropertyInfo propertyInfo = _propertyInfoResolver.GetPropertyInfo <TEntity>(filter.PropertyName); if (propertyInfo == null) { throw new ArgumentException($"Filtered entity has no field with name {filter.PropertyName}."); } IValueParser valueParser = _filterValueParserFactory.GetValueParser(propertyInfo); if (!valueParser.TryParse(filter.Value, out object parsedValue)) { throw new ArgumentException( $"Inappropriate filter value '{filter.Value}' for field with name {filter.PropertyName}."); } return(filter.Operation.GetOperationExpression(Expression.Property(entityParam, propertyInfo.Name), Expression.Constant( parsedValue, propertyInfo.PropertyType))); }