tags:

views:

200

answers:

1

FxCop is complaining about a event handler declaration I have. I don't see what is wrong with the code despite reading the warning several times.

Code in my user control

//This next line Fx Cops doesn't like.
public event ImageClickEventHandler NewEntity; //A thingy defined in the BCL

private void ImgBtnAdd_Click(object sender, ImageClickEventArgs e)
{
     NewEntity(sender, e);
}

Code in Page that uses the control

protected override void OnInit(EventArgs e)
{
EntitySearch1.NewEntity += EntitySearch1_NewEntity;
//etc.
}

But FxCop says:

By convention, .NET events have two parameters that specify the event sender and event data. Event handler signatures should follow this form: void MyEventHandler(object sender, EventArgs e). The 'sender' parameter is always of type System.Object, even if it is possible to employ a more specific type. The 'e' parameter is always of type System.EventArgs. Events that do not provide event data should use the System.EventHandler delegate type. Event handlers return void so that they can send each event to multiple target methods. Any value returned by a target would be lost after the first call.

+1  A: 

Looks bogus to me - the 'e' parameter certainly isn't always EventArgs; it's just conventionally a type which derives from EventArgs.

ImageClickEventArgs derives from EventArgs, so this looks fine to me. I suspect it's FxCop being a little stricter than it should be.

Jon Skeet