Beispiel #1
0
        public IEnumerable <ExpandoObject> Parse(Ssv format)
        {
            var tableNames = format.GetTablesNames().ToArray();

            foreach (var line in format.GetTableData(tableNames[0]))
            {
                IDictionary <string, object> obj = new ExpandoObject();

                var columnNamesLine = format.GetTableColumnNamesLine();
                var columnNames     = columnNamesLine.Values.Select(c => c.Trim()).ToArray();

                for (int i = 0; i < line.Values.Count; i++)
                {
                    var v = line.Values[i].Trim();
                    if (v.StartsWith("\"") && v.EndsWith("\""))
                    {
                        obj[columnNames[i]] = v;
                    }
                    else if (int.TryParse(v, out int result))
                    {
                        obj[columnNames[i]] = result;
                    }
                    else if (double.TryParse(v, out double dresult))
                    {
                        obj[columnNames[i]] = dresult;
                    }

                    obj[columnNames[i]] = v;
                }
                yield return((ExpandoObject)obj);
            }
        }
Beispiel #2
0
        public IEnumerable <T> Parse(Ssv format, Func <T> initialObjectProvider = null)
        {
            if (!Map.TryGetValue(typeof(T), out var tableMap))
            {
                if (!Map.TryGetValue(null, out tableMap))
                {
                    throw new Exception();
                }
            }

            var tableName  = tableMap.Item1;
            var properties = tableMap.Item2;

            foreach (var line in format.GetTableData(tableName))
            {
                var obj = initialObjectProvider == null?Activator.CreateInstance <T>() : initialObjectProvider();

                for (int i = 0; i < line.Values.Count; i++)
                {
                    if (i >= properties.Count)
                    {
                        break;
                    }
                    var propertyName = properties[i];
                    if (propertyName == null)
                    {
                        continue;
                    }
                    if (propertyName == string.Empty)
                    {
                        //take name from ssv
                        //todo
                    }
                    var propertyInfo       = obj.GetType().GetProperty(propertyName);
                    var formattedValue     = line.Values[i].Trim();
                    var formattedValueType = Convert.ChangeType(formattedValue, propertyInfo.PropertyType, CultureInfo.InvariantCulture);
                    propertyInfo.SetValue(obj, formattedValueType);
                }
                yield return(obj);
            }
        }