Well, it's interesting...
You can bind an eventhandler using a method group conversion with compatible types:
public void GenericHandlerMethod(object sender, EventArgs e) {}
...
// Valid
foo.FormClosingEvent += GenericHandlerMethod;
This will actually create an instance of FormClosingEventHandler
, not EventHandler
.
However you can't subscribe directly with an existing delegate of type EventHandler:
EventHandler genericHandler = GenericHandlerMethod;
// Invalid
foo.FormClosingEvent += genericHandler;
... but you can create a new delegate based on an existing one, if the types are compatible:
EventHandler generic = GenericHandlerMethod;
FormClosingEventHandler closingHandler = new FormClosingEventHandler(generic);
// Valid
foo.FormClosingEvent += closingHandler;
Basically you need to remember that all the syntactic sugar is effectively calling a method like this:
foo.AddFormClosingHandler(handler);
where the method has a signature of:
public void AddFormClosingHandler(FormClosingHandler handler)
Now remember that although they have compatible signatures, there's no reference conversion available from EventHandler
to FormClosingHandler
. It's not like one inherits from the other.
It gets even more confusing with generic covariance/contravariance, but we'll leave it there for now... hopefully that's given you something to chew on and options for working round the restrictions.