spouliot/gendarme — Issue #2: [Patch] DeclareEventsExplicitlyRule raised on generated code
Author: troberson
Created: 2012-01-25 19:42:31+00:00
State: open

The DeclareEventsExplicitlyRule is raised when using a lambda expression for an event.

using System;

sealed class Test
{
class TestEventArgs : EventArgs
{
public TestEventArgs (int x)
{
this.X = x;
}

public int X { get; private set; }
}

event EventHandler TestEvent;

public Test ()
{
TestEvent += (o,e) => Console.WriteLine ("Test: {0}", e.X); // <-------- Gendarme complains about this line

if (TestEvent != null)
TestEvent (this, new TestEventArgs (42));
}

static void Main ()
{
new Test ();
}
}
---

3. DeclareEventsExplicitlyRule

Problem: An event handler was declared without the 'event' keyword
* Severity: High, Confidence: High
* Target: Test
* Location: System.EventHandler`1 Test::<>f__am$cache0

Solution: Add the missing 'event' keyword to your event handler declaration
More info available at: https://github.com/spouliot/gendarme/wiki/Gendarme.Rules.Correctness.DeclareEventsExplicitlyRule(2.10)