Is the "sender" in Button_Click (the sender of the object ... really the sender?

In a podcast based on events, Ted Fyson noted that the sender and self objects in .NET, C ++, and Java Event Operators, such as:

private void Button_Click(object sender, RoutedEventArgs e)

are incorrect because, for example, in the above example, the “sender” is not really the object that generated the event, but a proxy server, since you do not want your applications to be tightly coupled.

I misunderstood it (because when I debug it, the "sender" really seems to be the original object).

Or is it that common event patterns in these languages ​​(for example, a regular click handler) are closely related, but they should be more decoupled, for example. in composite applications.

He also mentioned that, for example, you should not inherit from EventArgs, since this leads to an explosion of classes, one per event, which carries only a few variables. In his opinion, many times you can just send a string, for example. He noted that this view is the opposite of what Microsoft Patterns and Practices offers.

Any thoughts on these areas?

+3
source share
2 answers

sender - Button ( - ), . , - , (, ) pass-thru:

class Foo {
   private Bar bar;
   public Foo(Bar bar) {
       this.bar = bar;
   }
   public event EventHandler SomeEvent {
       add {bar.SomeEvent += value;}
       remove {bar.SomeEvent -= value;}
   }
   //...
}

, foo.SomeEvent, , Bar, sender foo. , Foo.SomeEvent .

, sender; , . , , , - , ( ).

Re EventArgs - ( ) . . , EventHandler<T>, . - , , ; EventArgs.

, ( MiscUtil Push LINQ) - , .

+1

- "" , . , , , ( - , ).

, eventargs, frmo EventArgs, - . EventArgs.Empty, , , , . , , - , , , , , , . , , , , , .

+1

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


All Articles