private static void SerialiseProps(TrivialNameValueFileFormat res, string prefix, object?obj) { if (obj == null) { return; } var t = obj.GetType(); var props = t.GetProperties(); foreach (var prop in props) { var v = prop.GetValue(obj, null); if (v == null) { continue; } var vt = v.GetType(); if (vt == typeof(string)) { res.Add(prefix + prop.Name, (string)v); } else if (vt == typeof(int)) { res.Add(prefix + prop.Name, v.ToString()); } else if (vt == typeof(DateTime)) { res.Add(prefix + prop.Name, v.ToString()); } else if (vt == typeof(TimeSpan)) { res.Add(prefix + prop.Name, v.ToString()); } else if (vt == typeof(bool)) { res.Add(prefix + prop.Name, v.ToString()); } else if (vt == typeof(int)) { res.Add(prefix + prop.Name, v.ToString()); } else { SerialiseProps(res, string.IsNullOrWhiteSpace(prefix) ? prop.Name + "." : prefix + "." + prop.Name + ".", v); } } }
public static TrivialNameValueFileFormat Load(string fileName) { var dict = new TrivialNameValueFileFormat(); foreach (var line in File.ReadAllLines(fileName)) { if (string.IsNullOrWhiteSpace(line) || line.StartsWith(Comment)) { continue; } var idx = SimpleSplit(line, line.IndexOf(Seperator)); if (idx != null) { dict.Add(idx.Item1, idx.Item2 !); } } return(dict); }