using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Text; using System.Linq; using System.Xml.Linq; using System.Diagnostics; namespace libThermoControl { public static class Configuration { private const string config_file = "config.xml"; public static readonly int MIN_TEMP = 0; // 60 public static readonly int MAX_TEMP = 0; // 90 public static readonly int DEFAULT_TEMP = 0; // 70 public static readonly string DEFAULT_MODE = ""; // cool public static readonly string NONE = ""; // cool [Conditional("CONFIG_UNITTEST")] public static void UnitTest() { } static Configuration() { // load configuration //using (FileStream fs = new FileStream(config_file, FileMode.Open, FileAccess.Read, FileShare.Read)) //{ //} XDocument xdoc = XDocument.Load(config_file); Type p = typeof(Configuration); var fields = new List(p.GetFields(BindingFlags.Public | BindingFlags.Static)); foreach (var field in fields) { //Console.WriteLine("Name: '{0}' value: '{1}'", field.Name, xdoc); //var lv1s = from lv1 in xdoc.Descendants("level1") // select lv1.Attribute("name").Value; //var t = xdoc.Root.DescendantNodesAndSelf().ToList(); var o = xdoc.Root.Elements(field.Name); var list = o.ToList(); if (list.Count == 0) { Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{1}'", field.Name, field.GetValue(null).ToString()); } else { var t = xdoc.Root.Elements(field.Name).ToList().FirstOrDefault().Value; Console.WriteLine("Name: '{0}' Default: '{1}' Configured: '{2}'", field.Name, field.GetValue(null).ToString(), t); field.SetValue(null, Convert.ChangeType(t,field.FieldType)); } } } } }