I want to add events to some built in .NET classes. For example I want to add the following events to a generic List(T)
:
- ItemAdded
- ItemRemoved
- ListChanged
- etc
I can think of a few ways to accomplish this, but I'm not sure which approach is best.
- I could use "extension events", that is, if there is such a thing or a way to use extension methods to simulate event-like functionality.
- I could inherit from
List(T)
and override the necessary methods and properties. - I could inherit from
List(T)
and shadow the necessary methods and properties. - I could create a new class that implements
IList(T)
and then wrap the remaining functionality ofList(T)
thatIList(T)
is missing.
Obviously option 4 is the most work, which is not a big deal if I only wanted to extend one class. However, I also want to avoid nasty side effects.
What are the advantages, disadvantages, and caveats for each approach?
Note: I'm not really concerned here with IList(T) specifically. I find myself wanting to add "extenstion events" to other classes as well.