Using ChoFileDictionaryConfigStorage
Download sample source files (Require .NET 4.0 / Visual Studio 2010)
In this section, I’ll go over the details of using ChoFileDictionaryConfigStorage in your application. It provides key/value-pair configuration information from a file source. It is the default storage used by ChoDictionaryConfigurationHandler.
1. Define the configuration section object decorated with ChoDictionaryConfigurationSectionAttribute as below.
[ChoDictionaryConfigurationSection("sample")]
public class SampleConfigSection : ChoConfigurableObject
{
#region Instance Data Members (Public)
[ChoPropertyInfo("name", DefaultValue="Mark")]
public string Name;
[ChoPropertyInfo("message", DefaultValue="Hello World!")]
public string Message;
#endregion
[ChoAfterConfigurationObjectLoadedHandler]
void OnAfterConfigurationObjectLoaded(object sender, ChoConfigurationObjectEventArgs e)
{
Console.WriteLine(sender.ToString());
}
}
2. Now instantiate and use it as below.
class Program
{
static void Main(string[] args)
{
SampleConfigSection sampleConfigSection = new SampleConfigSection();
Console.WriteLine(sampleConfigSection.ToString());
}
}
The configuration section will be generated automatically for the first time in HelloWorld.exe.xml as below
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="sample" type="Cinchoo.Core.Configuration.ChoDictionarySectionHandler, Cinchoo.Core" />
</configSections>
<sample>
<add key="name" value="Mark" />
<add key="message" value="Hello World!" />
</sample>
</configuration>