Beispiel #1
0
        public virtual bool Match(TModel model, string line)
        {
            if (TryParse != null)
            {
                TValue result;
                if (!TryParse.Invoke(line, out result))
                {
                    return(false);
                }
                SetActualValue(model, result);
                return(true);
            }
            var parts = line.Split(Separator, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length < 2)
            {
                return(false);
            }
            if (Key != parts[0])
            {
                return(false);
            }
            SetActualValue(model, GetValue(parts[1].Trim()));
            return(true);
        }
Beispiel #2
0
        public override bool Match(TModel model, string line)
        {
            var list = (List <TValue>)Property.GetValue(model);

            if (TryParse != null)
            {
                TValue result;
                if (!TryParse.Invoke(line, out result))
                {
                    return(false);
                }
                list.Add(result);
                return(true);
            }
            var parts = line.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length < 2)
            {
                return(false);
            }
            if (!parts[0].TrimStart().StartsWith(Key))
            {
                return(false);
            }
            var indexpart = Regex.Match(parts[0]).Value.Trim();
            var index     = int.Parse(indexpart);

            //list.Insert(index - 1, (TValue)GetValue(parts[1].Trim()));
            while (list.Count < index)
            {
                list.Add(default(TValue));
            }
            list[index - 1] = (TValue)GetValue(parts[1].Trim());
            return(true);
        }
        private static void SetValueInternal(this PropertyInfo propertyInfo, object obj, string value, bool isNullable, string typeString, TryParse tryParse)
        {
            object oValue = null;

            if (tryParse.Invoke(value, ref oValue))
            {
                propertyInfo.SetValue(obj, oValue, null);
                return;
            }
            else if (isNullable)
            {
                propertyInfo.SetValue(obj, null, null);
            }
            else
            {
                throw new ParseException(typeString, value);
            }
        }
 private T ParseOrDefault <T>(TryParse <T> tryParse) => tryParse.Invoke(Value, out T result) ? result : default(T);
Beispiel #5
0
 public static T TryParseOrDefault <T>(this string stringValue, TryParse <T> action)
 {
     return(action.Invoke(stringValue, out var value) ? value : default(T));
 }