.NET EventHandlers - Generic or no?

后端 未结 9 1804
遇见更好的自我
遇见更好的自我 2020-12-03 13:53

Every time I start in deep in a C# project, I end up with lots of events that really just need to pass a single item. I stick with the EventHandler/Event

相关标签:
9条回答
  • 2020-12-03 14:05

    This is the correct implementation. It has been added to the .NET Framework (mscorlib) since generics first came available (2.0).

    For more on its usage and implementation see MSDN: http://msdn.microsoft.com/en-us/library/db0etb8x.aspx

    0 讨论(0)
  • 2020-12-03 14:06

    The first time I saw this little pattern, I was using Composite UI Application block, from MS Patterns & Practices group.

    It doesn't throw any red flag to me ; in fact it is even a smart way of leveraging generics to follow the DRY rule.

    0 讨论(0)
  • 2020-12-03 14:14

    You can find Generic EventHandler on MSDN http://msdn.microsoft.com/en-us/library/db0etb8x.aspx

    I have been using generic EventHandler extensively and was able to prevent so-called "Explosion of Types(Classes)" Project was kept smaller and easier to navigate around.

    Coming up with a new intuitive a delegate for non-generic EventHandler delegate is painful and overlap with existing types Appending "*EventHandler" to new delegate name does not help much in my opinion

    0 讨论(0)
  • 2020-12-03 14:17

    No, I don't think this is the wrong approach. I think it's even recommended in the [fantastic] book Framework Design Guidelines. I do the same thing.

    0 讨论(0)
  • 2020-12-03 14:20

    Delegate of the following form has been added since .NET Framework 2.0

    public delegate void EventHandler<TArgs>(object sender, TArgs args) where TArgs : EventArgs
    

    You approach goes a bit further, since you provide out-of-the-box implementation for EventArgs with single data item, but it lacks several properties of the original idea:

    1. You cannot add more properties to the event data without changing dependent code. You will have to change the delegate signature to provide more data to the event subscriber.
    2. Your data object is generic, but it is also "anonymous", and while reading the code you will have to decipher the "Item" property from usages. It should be named according to the data it provides.
    3. Using generics this way you can't make parallel hierarchy of EventArgs, when you have hierarchy of underlying (item) types. E.g. EventArgs<BaseType> is not base type for EventArgs<DerivedType>, even if BaseType is base for DerivedType.

    So, I think it is better to use generic EventHandler<T>, but still have custom EventArgs classes, organized according to the requirements of the data model. With Visual Studio and extensions like ReSharper, it is only a matter of few commands to create new class like that.

    0 讨论(0)
  • 2020-12-03 14:25

    Use generic event handler instances

    Before .NET Framework 2.0, in order to pass custom information to the event handler, a new delegate had to be declared that specified a class derived from the System.EventArgs class. This is no longer true in .NET

    Framework 2.0, which introduced the System.EventHandler<T>) delegate. This generic delegate allows any class derived from EventArgs to be used with the event handler.

    0 讨论(0)
提交回复
热议问题