I have been using an XML configuration type setting for some time, which starts from the inline configuration, which is pretty neat. Basically, you can give it a hint and it will deserialize the xml directly into an object for you. I think the original source from which I got this was here , but I am not 100% sure, so if someone knows the author, edit his answer to give me the proper credit.
Here is a class that actually does deserialization:
using System.Configuration; using System.Xml; using System.Xml.Serialization; using System.Xml.XPath; namespace Ariel.Configuration { class XmlSerializerSectionHandler : IConfigurationSectionHandler { public object Create(object parent, object configContext, XmlNode section) { XPathNavigator nav = section.CreateNavigator(); string typename = (string)nav.Evaluate("string(@type)"); Type t = Type.GetType(typename); XmlSerializer ser = new XmlSerializer(t); return ser.Deserialize(new XmlNodeReader(section)); } } }
Now you can create any class you want to customize.
namespace MyProject.Config { public class TestConfig {
Now you need the app.config file. I usually prefer the style of using a separate configuration file for each configuration class, so I will give you this as an example.
<?xml version="1.0"?> <configuration> <configSections> <section name="TestConfig" type="Ariel.Configuration.XmlSerializerSectionHandler, Ariel"/> </configSections> <runtime> <gcServer enabled="true"/> </runtime> <TestConfig configSource="Config\\TestConfig.config" /> <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
The important parts are: section name = "TestConfig" <- Configuration class type = Ariel.Configuration.XmlSerializerSectionHandler, Ariel <- The type that is used for deserialization is the xml in the object. This basically tells the configuration manager what to run to create our TestConfig object configSource = "Config \ TestConfig.config" <- This tells us where to find the configuration file for the TestConfig section.
In the TestConfig.config file, you have something like this:
<?xml version="1.0" encoding="utf-8" ?> <TestConfig type="MyProject.Config.TestConfig, MyProject"> <Enabled>True</Enabled> </TestConfig>
Again, the important part is the reference to the class name and type.
Last but not least, load the configuration:
using System.Configuration; Config.SessionKillConfig config = null; try { config = (Config.SessionKillConfig)ConfigurationManager.GetSection("CdrAnalyzerConfig"); } catch (System.Configuration.ConfigurationException ex) { syslog.Fatal("Loading parser configuration failed", ex); return; } if(config.Enabled) {
It may be a little long, but the fact is that it is quite portable. Basically, to enable configuration, all you have to do is write a class and then write an xml file corresponding to this.
I am not 100% sure how to pass the configuration file as a parameter to the program, but I believe that in this case there is an option in the ConfigurationManager class. I even think I read somewhere that someone is working to detect changes in the file and reload the new configuration into the running program, but I have not tried anything like this. In any case, I hope this example is useful for one approach that you can take.