Merge pull request #799 from kebby/master
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConfigurationSectionCollection.cs
1 //
2 // System.Configuration.ConfigurationSectionCollection.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //  Lluis Sanchez Gual (lluis@novell.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
28 //
29
30 using System;
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Runtime.Serialization;
34
35 namespace System.Configuration
36 {
37         [Serializable]
38         public sealed class ConfigurationSectionCollection : NameObjectCollectionBase
39         {
40                 SectionGroupInfo group;
41                 Configuration config;
42                 
43                 internal ConfigurationSectionCollection (Configuration config, SectionGroupInfo group)
44                         : base (StringComparer.Ordinal)
45                 {
46                         this.config = config;
47                         this.group = group;
48                 }
49                 
50                 public override NameObjectCollectionBase.KeysCollection Keys
51                 {
52                         get { return group.Sections.Keys; }
53                 }
54                 
55                 public override int Count 
56                 {
57                         get { return group.Sections.Count; }
58                 }
59         
60                 public ConfigurationSection this [string name]
61                 {
62                         get {
63                                 ConfigurationSection sec = BaseGet (name) as ConfigurationSection;
64                                 if (sec == null) {
65                                         SectionInfo secData = group.Sections [name] as SectionInfo;
66                                         if (secData == null) return null;
67                                         sec = config.GetSectionInstance (secData, true);
68                                         if (sec == null) return null;
69                                         BaseSet (name, sec);
70                                 }
71                                 return sec;
72                         }
73                 }
74         
75                 public ConfigurationSection this [int index]
76                 {
77                         get { return this [GetKey (index)]; }
78                 }
79                 
80                 public void Add (string name, ConfigurationSection section)
81                 {
82                         config.CreateSection (group, name, section);
83                 }
84                 
85                 public void Clear ()
86                 {
87                         if (group.Sections != null) {
88                                 foreach (ConfigInfo data in group.Sections)
89                                         config.RemoveConfigInfo (data);
90                         }
91                 }
92                 
93                 public void CopyTo (ConfigurationSection [] array, int index)
94                 {
95                         for (int n=0; n<group.Sections.Count; n++)
96                                 array [n + index] = this [n];
97                 }
98                 
99                 public ConfigurationSection Get (int index)
100                 {
101                         return this [index];
102                 }
103                 
104                 public ConfigurationSection Get (string name)
105                 {
106                         return this [name];
107                 }
108                 
109                 public override IEnumerator GetEnumerator()
110                 {
111                         foreach (string key in group.Sections.AllKeys)
112                                 yield return this [key];
113                 }
114                 
115                 public string GetKey (int index)
116                 {
117                         return group.Sections.GetKey (index);
118                 }
119                 
120                 public void Remove (string name)
121                 {
122                         SectionInfo secData = group.Sections [name] as SectionInfo;
123                         if (secData != null)
124                                 config.RemoveConfigInfo (secData);
125                 }
126                 
127                 public void RemoveAt (int index)
128                 {
129                         SectionInfo secData = group.Sections [index] as SectionInfo;
130                         config.RemoveConfigInfo (secData);
131                 }
132
133                 [MonoTODO]
134                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
135                 {
136                         throw new NotImplementedException ();
137                 }
138         }
139 }
140