Suppose I want to do something like this:
class Foo
{
public event BarHandler Bar;
...
}
class FooList<T> : List<T> where T : Foo
{
public override void Add(T item)
{
item.Bar += Bar_Handler;
base.Add(item);
}
private void Bar_Handler ...
...
}
But Add
in List<T>
is not virtual, so I cannot use override
, I would have to resort to new
instead. This however does not provide polymorphism, and I am worried about subtle bugs that could be introduced by refering to FooList
as simply List
, which would lead to my Event-Handler not being added.
My current specific case of this is: I want to subclass ObservableCollection
for Items that implement INotifyPropertyChanged
, and add/remove an Event-Handler to those Items if they are added/removed. Then I provide an Event that is raised if any Item in Collection changes.
I would like a solution for my particular problem as well as the underlying general problem, as this is somethink I stumbled upon a few times, coming from a java background.