public static void ReadConfig(this Type type, string file) { char[] split = new char[] { delimiter }; if (File.Exists(file)) { using (StreamReader input = new StreamReader(file)) { string line; while ((line = input.ReadLine()) != null) { line = line.TrimStart(); if (line == "" || line.StartsWith(comment)) { continue; } string[] parts = line.Split(split, 2); string name = parts[0].Trim(); string value = parts[1]; Action <object> assign; Type attr_type; FieldInfo finfo = type.GetField(name, configPropFlags); if (finfo != null) { assign = obj => finfo.SetValue(null, obj); attr_type = finfo.FieldType; } else { PropertyInfo pinfo = type.GetProperty(name, configPropFlags); if (pinfo != null) { assign = obj => pinfo.SetValue(null, obj); attr_type = pinfo.PropertyType; } else { Console.WriteLine(string.Format("[Config] Unknown property {0} for class {1}", name, type.Name)); continue; } } object parsed = GenericIO.Parse(value, attr_type); assign(parsed); } } } }
public static void WriteConfig(this Type type, string file) { using (StreamWriter output = new StreamWriter(file)) { foreach (var info in type.GetFields <ConfiguredAttribute>(configPropFlags)) { string name = info.Name; string value = GenericIO.Serialize(info.GetValue(null)); output.WriteLine(string.Format(outputTemplate, name, value)); } foreach (var info in type.GetProperties <ConfiguredAttribute>(configPropFlags)) { string name = info.Name; string value = GenericIO.Serialize(info.GetValue(null)); output.WriteLine(string.Format(outputTemplate, name, value)); } } }