使用通用类型扩展集合

时间:2012-10-12 14:02:09

标签: c# .net

我的目标是扩展Collection对象以支持标记何时从Collection中添加,更新或删除项目。 MSDN article提供了针对特定类型进行扩展的示例(示例2)。我想保持Generic,所以我不必为我拥有的每个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));
    }

3 个答案:

答案 0 :(得分: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;
        }
    }

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

您必须将Change EventArgs类定义为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)));
        }
    }
}