-3

我的目标是扩展 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));
    }
4

3 回答 3

2

@daveL 是完全正确的,你想要的是一个通用事件参数:

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;
    }
}

但他在回答中遗漏的是如何使用它;首先让您的事件处理程序使用与T主类相同的类型:

class CollectionChangeTracked<T> : Collection<T>
{
    public event EventHandler<ChangedEventArgs<T>> Changed;

   ....

}

然后,您的个人方法,而不是重新定义类型,TParam应该使用T在类级别定义的:

protected override void InsertItem(int index, T newItem)
{
    base.InsertItem(index, newItem);
    EventHandler<ChangedEventArgs<T>> temp = Changed;
    if (temp != null)
    {
      // Added <T> after ChangedEventArgs  
      temp(this, new ChangedEventArgs<T>(ChangeType.Added, newItem, default(T)));
    }
}

现场示例:http ://rextester.com/XFCLN26271

于 2012-10-12T14:13:29.317 回答
2

我想这会有所帮助,但我有点猜测,因为您的问题含糊不清且未完成

 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;
        }
    }
于 2012-10-12T14:06:53.613 回答
1

您必须将 ChangedEventArgs 类定义为 Generic。

尝试这个:

public class ChangedEventArgs<T> : EventArgs
{
    public readonly T ChangedItem;
    public readonly ChangeType ChangeType;
    public readonly T ReplacedWith;

    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, default(T)));
        }
    }

    protected override void ClearItems()
    {
        base.ClearItems();

        EventHandler<ChangedEventArgs<T>> temp = Changed;
        if (temp != null)
        {
            temp(this, new ChangedEventArgs<T>(
                ChangeType.Cleared, default(T), default(T)));
        }
    }
}
于 2012-10-12T14:17:27.230 回答