So, wenn wir schonmal dabei sind, hier mein zweites Snippet für den heutigen Tag. Ist zwar nicht mehr ganz so aktuell, aber besser spät als nie.
Dieses Snippet ist für eine "WatchedList", also eine List, welche Event-gesteuert über Änderungen benachrichtigen kann. Dabei dient die ListEventArgs-Klasse zur näheren Identifikation der Änderung. Viel Spaß damit.
Und wie immer: Verbesserungsvorschläge, Kritiken => Willkommen
using System;
using System.Collections.Generic;
using System.Text;
namespace Pulse
{
/// <summary>
///
/// </summary>
public enum ListChangedType
{
/// <summary>
///
/// </summary>
Unknown = 0,
/// <summary>
///
/// </summary>
Inserted = 1,
/// <summary>
///
/// </summary>
Updated = 2,
/// <summary>
///
/// </summary>
Deleted = 3,
/// <summary>
///
/// </summary>
Cleared = 4
}
/// <summary>
///
/// </summary>
public sealed class ListEventArgs : EventArgs
{
/// <summary>
///
/// </summary>
/// <param name="ListChangedType"></param>
/// <param name="Index"></param>
/// <param name="Reference"></param>
internal ListEventArgs(ListChangedType ListChangedType, int Index, object Reference)
{
this.DataStore = new object[3];
this.DataStore[0] = ListChangedType;
this.DataStore[1] = Index;
this.DataStore[2] = Reference;
}
/// <summary>
/// Enthält die durchgeführte Aktion des Ereignisses
/// </summary>
public ListChangedType ListChangedType
{
get
{
return (ListChangedType)this.DataStore[0];
}
}
/// <summary>
/// Enthält den Index des zum Event gehörenden Elements
/// -1 bei Clear
/// </summary>
public int Index
{
get
{
return Pulse.Converter.ToInt(this.DataStore[1]);
}
}
/// <summary>
/// Enthält die Referenz auf das bei der Änderung betroffene Objekt
/// </summary>
public object Reference
{
get
{
return this.DataStore[2];
}
}
/// <summary>
/// Enthält die Daten
/// </summary>
private object[] DataStore;
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public sealed class WatchedList<T> : System.Collections.Generic.List<T>
{
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void ChangedEventHandler(object sender, ListEventArgs e);
/// <summary>
///
/// </summary>
public event ChangedEventHandler Changed;
/// <summary>
/// Invoke the Changed event; called whenever list changes
/// </summary>
/// <param name="e"></param>
private void OnChanged(ListEventArgs e)
{
if (Changed != null) Changed(this, e);
}
/// <summary>
/// Override some of the methods that can change the list;
/// invoke event after each
/// </summary>
/// <param name="item"></param>
public new void Add(T item)
{
base.Add(item);
this.OnChanged(new ListEventArgs(ListChangedType.Inserted, (base.Count - 1), item));
return;
}
/// <summary>
///
/// </summary>
public new void Clear()
{
base.Clear();
this.OnChanged(new ListEventArgs(ListChangedType.Cleared,-1, null));
}
/// <summary>
///
/// </summary>
/// <param name="Index"></param>
/// <returns></returns>
public new T this[int Index]
{
get
{
return base[Index];
}
set
{
base[Index] = value;
this.OnChanged(new ListEventArgs(ListChangedType.Updated, Index, value));
}
}
/// <summary>
///
/// </summary>
/// <typeparam name="TOutput"></typeparam>
/// <param name="converter"></param>
/// <returns></returns>
public new Pulse.WatchedList<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter)
{
Pulse.WatchedList<TOutput> Output = new Pulse.WatchedList<TOutput>();
foreach (TOutput Element in base.ConvertAll<TOutput>(converter)) Output.Add(Element);
return Output;
}
/// <summary>
///
/// </summary>
/// <param name="item"></param>
public new void Remove(T item)
{
int Index = base.IndexOf(item);
base.RemoveAt(Index);
this.OnChanged(new ListEventArgs(ListChangedType.Deleted, Index, item));
}
/// <summary>
///
/// </summary>
/// <param name="Index"></param>
public new void RemoveAt(int Index)
{
T Temp = this[Index];
base.RemoveAt(Index);
this.OnChanged(new ListEventArgs(ListChangedType.Deleted, Index, Temp));
}
/// <summary>
///
/// </summary>
/// <param name="Index"></param>
/// <param name="item"></param>
public new void Insert(int Index, T item)
{
base.Insert(Index, item);
this.OnChanged(new ListEventArgs(ListChangedType.Inserted, Index, item));
}
}
}
7 Kommentare zum Snippet