Tari has a neat post over at his blog about treating configuration as code with Python’s import hooks. I thought the way he handled it was really neat--basically, he loads in an INI file, and it creates a dynamic object with properties equal to the INI file. So, his example was a file such as:
Code:
And it can be accessed like:
Code:
Well, I thought this was really cool and wanted to do something similar in C#. I found a neat class called ExpandoObject that would allow for this sort of thing to C#. So, for example, given the above INI file you could do:
Code:
And it would output "meow". Because of the way the objects work, it's also trivial to iterate over the properties and such. Here's a screen shot showing more advanced output with a slightly larger INI file:
The code to do that is very simple:
Code:
You can download the dll as well as the source and test program at:
http://merthsoft.com/DynamicConfig.zip (requires .net 4.0)
The INI loading is very primitive right now. It handles quoted values, but not escape characters. Start a line with ";" or "#" for comments.
Feel free to do whatever you want with it--I plan on adding it to TokenIDE for some better configuration of fonts and such, unless I change my mind and use XML.
Code:
[cat]
sound=meow
[dog]
sound=woof
[cow]
sound=moo
And it can be accessed like:
Code:
>>> import INIImport
>>> INIImport.init()
>>> from config import foo
>>> foo.cat
{'sound': 'meow'}
>>> foo.dog['sound']
'woof'
Well, I thought this was really cool and wanted to do something similar in C#. I found a neat class called ExpandoObject that would allow for this sort of thing to C#. So, for example, given the above INI file you could do:
Code:
dynamic foo = Config.ReadIni("Config.ini");
Console.WriteLine(foo.cat.sound);
And it would output "meow". Because of the way the objects work, it's also trivial to iterate over the properties and such. Here's a screen shot showing more advanced output with a slightly larger INI file:
The code to do that is very simple:
Code:
static void Main(string[] args) {
// Read in the INI file as a dynamic type.
dynamic config = Config.ReadIni("Config.ini");
// Access the properties directly as if it were an object.
Console.WriteLine("Accessing properties directly:");
Console.WriteLine("Cats say {0}.", config.cat.sound);
Console.WriteLine("Dogs say {0}.", config.dog.sound);
Console.WriteLine("Cows say {0}.", config.cow.sound);
Console.WriteLine();
// Because we're using ExpandoObjects, which implement IDictionary<string, object>,
// we can also iterate over the collection. Each item in the config is another ExpandoObject
// meaning we can iterate over that as well.
Console.WriteLine("Iterating over properties:");
foreach (var prop in ((IDictionary<string, object>)config)) {
Console.WriteLine("A {0}:", prop.Key);
foreach (var key in ((IDictionary<string, object>)prop.Value)) {
Console.WriteLine("\t{0}={1}", key.Key, key.Value);
}
}
Console.ReadKey();
}
You can download the dll as well as the source and test program at:
http://merthsoft.com/DynamicConfig.zip (requires .net 4.0)
The INI loading is very primitive right now. It handles quoted values, but not escape characters. Start a line with ";" or "#" for comments.
Feel free to do whatever you want with it--I plan on adding it to TokenIDE for some better configuration of fonts and such, unless I change my mind and use XML.