code_junkie

Evil use of extension methods?

Nov 12th, 2011
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. public static void Raise(this EventHandler handler, object sender, EventArgs args)
  2. {
  3. if (handler != null)
  4. {
  5. handler(sender, args);
  6. }
  7. }
  8.  
  9. // Usage:
  10. MyButtonClicked.Raise(this, EventArgs.Empty);
  11.  
  12. // This works too! Evil?
  13. EventHandler handler = null;
  14. handler.Raise(this, EVentArgs.Empty);
  15.  
  16. public event EventHandler<EventArgs> OnClicked = delegate { };
  17.  
  18. public static void Raise(this EventHandler handler, object sender)
  19. {
  20. Raise(handler, sender, EventArgs.Empty);
  21. }
  22.  
  23. public static void Raise<TA>(this EventHandler<TA> handler, object sender, TA args)
  24. where TA : EventArgs
  25. {
  26. if (handler != null)
  27. {
  28. handler(sender, args);
  29. }
  30. }
  31.  
  32. protected virtual OnSomeEvent(EventArgs e){ }
  33.  
  34. if (myEvent!= null) {
  35. myEvent(this, new EventArgs());
  36. }
Add Comment
Please, Sign In to add comment