Well, the PickledHedgehog Config Section Generator does exactly what it says on the tin! It generates the classes for your Configuration Section, FROM XML! You slap in the namespace, the xml, and off it goes...
I will add more info about this later...
Clicky
I cant gurantee how well this thing will work. I really cant
I have (tried) to setup an issue tracker -
Software Correction Requests for the CSG. Please log the bugs in there (if you can)
I have only tested this thing with small bits of XML, but, as far as I can tell, complex bits:
1 <blah>
2 <Group>
3 <G name="one"/>
4 <G name="two"/>
5 </Group>
6 </blah>
And it has generated the following(Excuse the bad naming, i have been working on this till 5am for the last 3 weekends):
1 namespace blah2
2 {
3 using System.Configuration;
4
5 public class blah : ConfigurationSection
6 {
7 [ConfigurationProperty("Group")]
8 public virtual GroupCollection Group
9 {
10 get
11 {
12 return (GroupCollection)this["Group"];
13 }
14 }
15 }
16 }
17
18 namespace blah2
19 {
20 using System.Configuration;
21
22 public class GroupCollection : ConfigurationElementCollection
23 {
24 protected override object GetElementKey(ConfigurationElement element)
25 {
26 return element.GetHashCode();
27 }
28 public virtual G this[int index]
29 {
30 get
31 {
32 return (G)BaseGet(index);
33 }
34 }
35 protected override string ElementName
36 {
37 get
38 {
39 return "G";
40 }
41 }
42 protected override ConfigurationElement CreateNewElement()
43 {
44 return new G();
45 }
46 public override ConfigurationElementCollectionType CollectionType
47 {
48 get
49 {
50 return ConfigurationElementCollectionType.BasicMap;
51 }
52 }
53 }
54 }
55
56 namespace blah2
57 {
58 using System.Configuration;
59
60 public class G : ConfigurationElement
61 {
62 [ConfigurationProperty("name")]
63 public virtual string name
64 {
65 get
66 {
67 return (System.String)this["name"];
68 }
69 }
70 }
71 }
72
If there are multiple of the same key, it will generate the collection for you. At the moment, you can only have one type of collection (I _think_ thats a limitation of .net, not sure), and some parts may not function at all...
The code to "test" this bit of code was simple:
1 Response.Write(blah.Group[1].name);
And it outputted "two", which is correct
A smallish update - iv tried to fix a potential problem with nesting collections inside one another - sofar my tests have seemed to work. And I have also added
dp.SyntaxHighlighter to the output to make it look purty.
IF you have issues with the generation, please email me with the xml and the code generated, and I gurantee I will have a look at it.