* ConfigurationManagerTest.cs: Added/improved tests for
[mono.git] / mcs / class / System.Configuration / Test / standalone / t40.cs
1 using System;
2 using System.Configuration;
3
4 public class MyElement : ConfigurationElement
5 {
6         public MyElement ()
7         {
8         }
9
10         [ConfigurationProperty ("name", Options = ConfigurationPropertyOptions.IsKey)]
11         public string Name {
12                 get { return (string) this ["name"]; }
13         }
14         [ConfigurationProperty ("value")]
15         public string Value {
16                 get { return (string) this ["value"]; }
17         }
18 }
19
20 [ConfigurationCollection (typeof (MyElement), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMapAlternate)]
21 public class MyElementCollection : ConfigurationElementCollection
22 {
23         protected override ConfigurationElement CreateNewElement ()
24         {
25                 return new MyElement ();
26         }
27         protected override object GetElementKey (ConfigurationElement e)
28         {
29                 return ((MyElement) e).Name;
30         }
31
32         public void Add (MyElement e)
33         {
34                 BaseAdd (e);
35         }
36
37         protected override void BaseAdd (ConfigurationElement e)
38         {
39                 base.BaseAdd (e);
40         }
41 }
42
43 public class MySection : ConfigurationSection
44 {
45         [ConfigurationProperty ("MyElements")]
46         public MyElementCollection MyElements {
47                 get { return (MyElementCollection) this ["MyElements"]; }
48         }
49 }
50
51 public class Driver
52 {
53         public static void Main ()
54         {
55                 try {
56                         MySection ms = (MySection) ConfigurationManager.GetSection ("MySection");
57                         foreach (MyElement e in ms.MyElements)
58                                 Console.WriteLine (e.Name);
59                 } catch (ConfigurationException ex) {
60                         Console.WriteLine ("Error.");
61                 }
62         }
63 }
64