static object ReadProperties(this TextReader reader, FileSection section) { var properties = section.CreateObject(); if (!reader.TryReadHeader(section)) { throw new FormatException($"Section {section.Text} not found or out of order."); } var fields = section.Fields .ToDictionary(f => f.Name); while (reader.TryReadProperty(out var name, out var value)) { fields[name][properties] = Convert.ChangeType(value, fields[name].Type); } return(properties); }
static bool TryReadRow(this TextReader reader, FileSection section, out object row) { row = section.CreateObject(); var line = reader.ReadLine()?.Trim(); if (string.IsNullOrWhiteSpace(line)) { return(false); } var values = line.Split('|') .Select((v, i) => new { Value = v.Trim(), Index = i }) .ToDictionary(x => x.Index, x => x.Value); foreach (var x in section.Fields.Select((f, i) => new { Field = f, Index = i })) { x.Field[row] = Convert.ChangeType(values[x.Index], x.Field.Type); } return(true); }