-
Notifications
You must be signed in to change notification settings - Fork 1
/
NamedElementsCollection.cs
97 lines (82 loc) · 1.98 KB
/
NamedElementsCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Grammophone.Configuration
{
/// <summary>
/// A container for configuratin elements deriving from <see cref="NamedElement"/>.
/// </summary>
/// <typeparam name="E">The type of elements, deriving from <see cref="NamedElement"/>.</typeparam>
public class NamedElementsCollection<E> : ConfigurationElementCollection
where E : NamedElement, new()
{
#region Public properties
/// <summary>
/// Get a contained element by index.
/// </summary>
public E this[int index]
{
get
{
return (E)BaseGet(index);
}
}
/// <summary>
/// Get a contained element by <see cref="NamedElement.Name"/>
/// or null if no such element exists.
/// </summary>
public new E this[string name]
{
get
{
return (E)BaseGet(name);
}
}
#endregion
#region Public methods
/// <summary>
/// Add an element to the collection.
/// </summary>
public void Add(E element)
{
if (element == null) throw new ArgumentNullException(nameof(element));
BaseAdd(element);
}
/// <summary>
/// Remove an element having a specified <see cref="NamedElement.Name"/>.
/// </summary>
public void Remove(string name)
{
if (name == null) throw new ArgumentNullException(nameof(name));
BaseRemove(name);
}
/// <summary>
/// Clear all elements.
/// </summary>
public void Clear()
{
BaseClear();
}
#endregion
#region Protected methods
/// <summary>
/// Creates a new element of type <typeparamref name="E"/>.
/// </summary>
protected override ConfigurationElement CreateNewElement()
{
return new E();
}
/// <summary>
/// Expects an element of type <typeparamref name="E"/> and
/// gets its <see cref="NamedElement.Name"/> property.
/// </summary>
protected override object GetElementKey(ConfigurationElement element)
{
return ((E)element).Name;
}
#endregion
}
}