Why are .NET .NET event handlers not universal?

I have been wondering for a while; but especially since over the past few weeks I have focused more on developing interfaces. This may sound like a broad question, but hopefully there is an answer or reason:

Why aren't .NET web control event handlers running?

Reasoning

The reason I ask is due to the subtlety and elegance of strictly typed event handlers. In my entire project, where necessary, I usually use the .NET EventHandler<T> delegate that exists with .NET 2.0; like here .

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

It would be relatively straightforward to expand on this and define the type for sender , something like this.

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

Whenever I work with .NET controls, sometimes I bind the event handler in the code, not in the ASPX file, and then, if necessary, throw the object on the desired type if I need to perform additional checks or changes.

Existing

Definition

 public class Button : WebControl, IButtonControl, IPostBackEventHandler { public event EventHandler Click; } 

Implementation

 protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.MyButton.Click += new EventHandler(MyButton_Click); } protected void MyButton_Click(object sender, EventArgs e) { // type cast and do whatever we need to do... Button myButton = sender as Button; } 

Generic

Definition

 public class Button : WebControl, IButtonControl, IPostBackEventHandler { public event EventHandler<Button, EventArgs> Click; } 

Implementation

 protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.MyButton.Click += new EventHandler(MyButton_Click); } protected void MyButton_Click(Button sender, EventArgs e) { // no need to type cast, yay! } 

I know this is a relatively small change, but of course it is more elegant? :)

+4
source share
1 answer

Because he is old.

Web controls were developed for .NET 1.0, and generics did not reach .NET 2.0.

Of course, the controls could be changed, but this means that all the old code will need to be changed for compilation (and they will need to be recompiled, since the old binaries will no longer work), and all the old examples (millions of web pages) are out of date.

+5
source

Source: https://habr.com/ru/post/1414864/


All Articles