locked
Checking if a method is a event handler RRS feed

  • Question

  • What's the best way for checking if a method is a event handler in a custom rule (VB.NET)? Is there an easier way then for each method to check all other methods and properties in the current class if they add this method as an event handler?

    thanks
    pascal
    Tuesday, March 16, 2010 12:55 PM

Answers

  • > What's the best way for checking if a method is a event handler

    You are right, this is tricky!  The code that does the add is not necessarily guaranteed to be in the current class, although it often will be.

    You might consider the following: consider a method to be an event handler if its first parameter is of type object and its second parameter is of type System.EventArgs or a subclass thereof.  This seems like it should be pretty accurate, assuming your code follows this standard convention for declaring event handlers.  (Regarding false positives:  I can't think of a time when I've had a method declared this way that was not an event handler.  Although maybe because I subconsciously avoid making methods that are not event handlers look like event handlers...)
    • Marked as answer by pascal.berger Wednesday, March 17, 2010 8:11 AM
    Wednesday, March 17, 2010 12:25 AM

All replies

  • > What's the best way for checking if a method is a event handler

    You are right, this is tricky!  The code that does the add is not necessarily guaranteed to be in the current class, although it often will be.

    You might consider the following: consider a method to be an event handler if its first parameter is of type object and its second parameter is of type System.EventArgs or a subclass thereof.  This seems like it should be pretty accurate, assuming your code follows this standard convention for declaring event handlers.  (Regarding false positives:  I can't think of a time when I've had a method declared this way that was not an event handler.  Although maybe because I subconsciously avoid making methods that are not event handlers look like event handlers...)
    • Marked as answer by pascal.berger Wednesday, March 17, 2010 8:11 AM
    Wednesday, March 17, 2010 12:25 AM
  • > The code that does the add is not necessarily guaranteed to be in the current class, although it often will be.
    I know, but I could live with that restriction.

    > You might consider the following: consider a method to be an event handler if its first parameter is of type object and its second parameter is
    > of type System.EventArgs or a subclass thereof.
    That's exactly what I'm doing at the moment. Unfortunately not all events stick to the guidelines and sometimes miss one of the parameters... But perhaps it's the easiest way to clean them up and use this approach.

    Thanks
    Pascal
    • Proposed as answer by Pascal Berger Wednesday, April 14, 2010 1:40 PM
    Wednesday, March 17, 2010 8:11 AM
  • Just found the IsEventHandling method in the RuleUtilities class (Microsoft.FxCop.Sdk namespace) which does the checking for the count and types of the arguments for determing if a method is an event handler.
    • Proposed as answer by Pascal Berger Wednesday, April 14, 2010 1:42 PM
    Wednesday, April 14, 2010 1:42 PM