2 using System.Collections.Generic;
12 private Dictionary<string, T> _NameDict =
new Dictionary<string, T>();
18 public T
this[
string Name]
20 get {
return _NameDict[Name]; }
31 return _NameDict.ContainsKey(Name);
40 Sort((T a, T b) => (a.Name.CompareTo(b.Name)));
44 #region Event handling
46 void NamedItemList_BeforeClear(
object sender, EventArgs e)
48 foreach (T Item
in this)
50 Item.BeforeNameChanged -=
new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
51 Item.AfterNameChanged -=
new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
56 void Item_AfterNameChanged(
object sender, NameChangeEventArgs e)
59 _NameDict.Remove(e.OldName);
61 _NameDict.Add(e.NewName, (T)sender);
65 void Item_BeforeNameChange(
object sender, NameChangeEventArgs e)
67 if (e.NewName != e.OldName)
69 if (_NameDict.ContainsKey(e.NewName))
71 throw new ArgumentException(
"Cant the the name of the INamedItem from {0} to {1}, because the new name does already exist in a colelction to which this object is assigned.".Build(e.OldName, e.NewName));
77 void NamedItemList_AfterRemove(
object sender, RemoveEventArgs<T> e)
79 if (_NameDict.ContainsKey(e.Item.Name))
81 _NameDict.Remove(e.Item.Name);
83 e.Item.BeforeNameChanged -=
new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
84 e.Item.AfterNameChanged -=
new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
88 void NamedItemList_AfterInsert(
object sender, InsertEventArgs<T> e)
90 _NameDict.Add(e.Item.Name, e.Item);
91 e.Item.BeforeNameChanged +=
new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
92 e.Item.AfterNameChanged +=
new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
97 void NamedItemList_BeforeInsert(
object sender, InsertEventArgs<T> e)
99 if (_NameDict.ContainsKey(e.Item.Name))
101 throw new ArgumentException(
"Cant insert the INamedItem named {0}. The name does already exist in the collection.".Build(e.Item.Name));
105 void NamedItemList_AfterSet(
object sender, SetEventArgs<T> e)
107 if (_NameDict.ContainsKey(e.OldItem.Name))
109 _NameDict.Remove(e.OldItem.Name);
111 _NameDict.Add(e.NewItem.Name, e.NewItem);
114 e.OldItem.BeforeNameChanged -=
new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
115 e.OldItem.AfterNameChanged -=
new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
117 e.NewItem.BeforeNameChanged +=
new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
118 e.NewItem.AfterNameChanged +=
new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
122 void NamedItemList_BeforeSet(
object sender, SetEventArgs<T> e)
124 if (!EqualityComparer<T>.Default.Equals(e.NewItem, e.OldItem))
126 if (!_NameDict.ContainsKey(e.NewItem.Name))
130 else if (_NameDict.ContainsKey(e.NewItem.Name) && EqualityComparer<T>.Default.Equals(e.OldItem, _NameDict[e.NewItem.Name]))
136 throw new ArgumentException(
"Cant set the INamedItem. The name {0} does already exist in the collection.".Build(e.NewItem.Name));
155 this.BeforeInsert +=
new EventHandler<InsertEventArgs<T>>(NamedItemList_BeforeInsert);
156 this.AfterInsert +=
new EventHandler<InsertEventArgs<T>>(NamedItemList_AfterInsert);
157 this.BeforeSet +=
new EventHandler<SetEventArgs<T>>(NamedItemList_BeforeSet);
158 this.AfterSet +=
new EventHandler<SetEventArgs<T>>(NamedItemList_AfterSet);
159 this.AfterRemove +=
new EventHandler<RemoveEventArgs<T>>(NamedItemList_AfterRemove);
160 this.BeforeClear +=
new EventHandler<EventArgs>(NamedItemList_BeforeClear);
172 AddRange(EnumerableList);
181 foreach (T Item
in this)
183 Item.BeforeNameChanged -=
new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
184 Item.AfterNameChanged -=
new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
bool Contains(string Name)
Checks if a INamedItem object with the specified name exists in the list.
Interface for items which can be added the the NamedItemList.
new void Sort()
Sorts the NamedItemList by the Name of the items.
NamedItemList(IEnumerable< T > EnumerableList)
Initializes a new instance of the NamedItemList class.
A list of uniquely named items which can be referenced by their name.
Extended version of the generic List class supporting events for various actions on the list...
NamedItemList()
Initializes a new instance of the NamedItemList class.