2 // This file is part of the LWES .NET Binding (LWES.net)
4 // COPYRIGHT© 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
5 // original .NET implementation
7 // LWES.net is free software: you can redistribute it and/or modify
8 // it under the terms of the Lesser GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // LWES.net is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // Lesser GNU General Public License for more details.
17 // You should have received a copy of the Lesser GNU General Public License
18 // along with LWES.net. If not, see <http://www.gnu.org/licenses/>.
20 namespace Org
.Lwes
.Config
22 using System
.Configuration
;
25 /// Base class for configuration element collections.
27 /// <typeparam name="TElement">Element type</typeparam>
28 /// <typeparam name="TKey">Key type</typeparam>
29 public abstract class AbstractConfigurationElementCollection
<TElement
, TKey
> : ConfigurationElementCollection
30 where TElement
: ConfigurationElement
, new()
35 /// Creates a new instance.
37 protected AbstractConfigurationElementCollection()
42 /// Creates a new instance.
44 /// <param name="addElmName">name used to add an element to the collection (default is 'add')</param>
45 /// <param name="clearElmName">name used when clearing elements from the collection (default is 'clear')</param>
46 /// <param name="removeElmName">name used to delete an element from the collection (default is 'remove')</param>
47 protected AbstractConfigurationElementCollection(string addElmName
49 , string removeElmName
)
51 base.AddElementName
= addElmName
;
52 base.ClearElementName
= clearElmName
;
53 base.RemoveElementName
= removeElmName
;
56 #endregion Constructors
63 public override ConfigurationElementCollectionType CollectionType
65 get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
69 /// Number of elements.
73 get { return base.Count; }
81 /// Accesses an element by index.
83 /// <param name="index">element index</param>
84 /// <returns>the element at <paramref name="index"/></returns>
85 public TElement
this[int index
]
87 get { return (TElement)BaseGet(index); }
90 if (BaseGet(index
) != null)
94 BaseAdd(index
, value);
99 /// Accesses a element by key.
101 /// <param name="key">an element's key</param>
102 /// <returns>the element with the given key</returns>
103 public TElement
this[TKey key
]
105 get { return (TElement)BaseGet(key); }
115 /// <param name="item"></param>
116 public void Add(TElement item
)
122 /// Clears the elements.
130 /// Finds the index of an element.
132 /// <param name="item">the element</param>
133 /// <returns>the index of the element</returns>
134 public int IndexOf(TElement item
)
136 return BaseIndexOf(item
);
140 /// Removes an element.
142 /// <param name="item">the element</param>
143 public void Remove(TElement item
)
145 BaseRemove(GetElementKey(item
));
149 /// Removes an element by key.
151 /// <param name="key">the element's key</param>
152 public void Remove(TKey key
)
158 /// Removes an element at the given index.
160 /// <param name="index">the element's index</param>
161 public void RemoveAt(int index
)
167 /// Creates a new element of type TElement.
169 /// <returns></returns>
170 protected override ConfigurationElement
CreateNewElement()
172 return new TElement();
176 /// Gets the element's key.
178 /// <param name="element">the element</param>
179 /// <returns>the element's key</returns>
180 protected override object GetElementKey(ConfigurationElement element
)
182 return PerformGetElementKey((TElement
)element
);
186 /// Abstract method; gets the element's key.
188 /// <param name="element">the element</param>
189 /// <returns>the element's key</returns>
190 protected abstract TKey
PerformGetElementKey(TElement element
);