我的目标是扩展 Collection 对象以支持在从 Collection 中添加、更新或删除项目时进行标记。MSDN 文章提供了针对特定类型进行扩展的示例(示例 2)。我想保持它是通用的,所以我不必为我拥有的每个 Collection 实现一个新类。
这是我到目前为止所拥有的:
public class ChangedEventArgs<T> : EventArgs
{
public readonly T ChangedItem;
public readonly T ReplacedWith;
public readonly ChangeType ChangeType;
public ChangedEventArgs(ChangeType change, T item, T replacement)
{
ChangeType = change;
ChangedItem = item;
ReplacedWith = replacement;
}
}
public enum ChangeType
{
Added,
Removed,
Replaced,
Cleared
};
class CollectionChangeTracked<T> : Collection<T>
{
public event EventHandler<ChangedEventArgs<T>> Changed;
protected override void InsertItem<TParam>(int index, TParam newItem)
{
base.InsertItem(index, newItem);
EventHandler<ChangedEventArgs<T>> temp = Changed;
if (temp != null)
{
temp(this, new ChangedEventArgs<T>(ChangeType.Added, newItem, null));
}
}
protected override void SetItem<TParam>(int index, TParam newItem)
{
string replaced = Items[index];
base.SetItem(index, newItem);
EventHandler<ChangedEventArgs<T>> temp = Changed;
if (temp != null)
{
temp(this, new ChangedEventArgs(ChangeType.Replaced, replaced, newItem));
}
}
protected override void RemoveItem<TParam>(int index)
{
TParam removedItem = Items[index];
base.RemoveItem(index);
EventHandler<ChangedEventArgs<T>> temp = Changed;
if (temp != null)
{
temp(this, new ChangedEventArgs<T>(ChangeType.Removed, removedItem, null));
}
}
protected override void ClearItems()
{
base.ClearItems();
EventHandler<ChangedEventArgs<T>> temp = Changed;
if (temp != null)
{
temp(this, new ChangedEventArgs(ChangeType.Cleared, null, null));
}
}
}
问题:我收到一条错误消息:base.InsertItem(index, newItem) 它说我的参数无效,但仅此而已。SetItem 和 RemoveItem 中的相同问题。
更新的编译代码:
public class ChangedEventArgs<T> : EventArgs
{
public readonly T ChangedItem;
public readonly T? ReplacedWith;
public readonly ChangeType ChangeType;
public ChangedEventArgs(ChangeType change, T item, T? replacement)
{
ChangeType = change;
ChangedItem = item;
ReplacedWith = replacement;
}
}
public enum ChangeType
{
Added,
Removed,
Replaced,
Cleared
};
class CollectionChangeTracked<T> : Collection<T>
{
public event EventHandler<ChangedEventArgs<T>> Changed;
protected override void InsertItem(int index, T newItem)
{
base.InsertItem(index, newItem);
EventHandler<ChangedEventArgs<T>> temp = Changed;
if (temp != null)
temp(this, new ChangedEventArgs<T>(ChangeType.Added, newItem, default(T)));
}
protected override void SetItem(int index, T newItem)
{
T replaced = Items[index];
base.SetItem(index, newItem);
EventHandler<ChangedEventArgs<T>> temp = Changed;
if (temp != null)
temp(this, new ChangedEventArgs<T>(ChangeType.Replaced, replaced, newItem));
}
protected override void RemoveItem(int index)
{
T removedItem = Items[index];
base.RemoveItem(index);
EventHandler<ChangedEventArgs<T>> temp = Changed;
if (temp != null)
temp(this, new ChangedEventArgs<T>(ChangeType.Removed, removedItem, null));
}
protected override void ClearItems()
{
base.ClearItems();
EventHandler<ChangedEventArgs<T>> temp = Changed;
if (temp != null)
temp(this, new ChangedEventArgs<T>(ChangeType.Cleared, null, null));
}