.NET (C#) events - custom EventArgs question

.net, c#, events

Solution

I've been done this road before. I went with your second idea of having a base event class that wraps common data. If no event will directly use it, make the base class internal to avoid exposing unnecessary objects to users. Having the base class also allows you to add more event data at a later time. Since the class is internal, users of it are outwardly unaffected by changes.

Problem

I have a number of C# custom events that are associated with items being selected in a tree and then a context menu being used to trigger some action on the selected items. I started creating a separate EventArgs-based class for each custom event, to package the necessary data for the event. In hind-sight though, I realize that most (maybe all) of my custom events will need to pass at least the list of underlying objects represented by the tree selection. Some events will likely need additional data as well. With that in mind, I wonder if either of the following is an acceptable practice? Use the same custom EventArgs-based class for multiple events (those that just need the object list to be passed). Obviously, that should work, but seems to break away from some of the recommended naming conventions used for wiring the event machinery. Create a base class that wraps up my often-needed object list, and then derive additional classes from it as additional data is needed. Maybe something else entirely? Currently, I only have a handful of custom events, but need to add more. Since I see a pattern emerging with regard to the data needed by each event, I'd like to have a better plan before continuing. Thanks for any advice.

Original source