2 using System.Configuration;
4 public class MyElement : ConfigurationElement
10 [ConfigurationProperty ("name", Options = ConfigurationPropertyOptions.IsKey)]
12 get { return (string) this ["name"]; }
14 [ConfigurationProperty ("value")]
16 get { return (string) this ["value"]; }
20 [ConfigurationCollection (typeof (MyElement), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMapAlternate)]
21 public class MyElementCollection : ConfigurationElementCollection
23 protected override ConfigurationElement CreateNewElement ()
25 return new MyElement ();
27 protected override object GetElementKey (ConfigurationElement e)
29 return ((MyElement) e).Name;
32 public void Add (MyElement e)
37 protected override void BaseAdd (ConfigurationElement e)
43 public class MySection : ConfigurationSection
45 [ConfigurationProperty ("MyElements")]
46 public MyElementCollection MyElements {
47 get { return (MyElementCollection) this ["MyElements"]; }
53 public static void Main ()
55 MySection ms = (MySection) ConfigurationManager.GetSection ("MySection");
56 foreach (MyElement e in ms.MyElements)
57 Console.WriteLine (e.Name);