CA1003: Use generic event handler instances
|
TypeName |
UseGenericEventHandlerInstances |
|
CheckId |
CA1003 |
|
Category |
Microsoft.Design |
|
Breaking Change |
Breaking |
Before .NET Framework 2.0, in order to pass custom information to the event handler, a new delegate had to be declared that specified a class that was derived from the System.EventArgs class. This is no longer true in .NET Framework 2.0, which introduced the System.EventHandler<TEventArgs> delegate. This generic delegate allows any class that is derived from EventArgs to be used together with the event handler.
To fix a violation of this rule, remove the delegate and replace its use by using the System.EventHandler<TEventArgs> delegate. If the delegate is autogenerated by the Visual Basic compiler, change the syntax of the event declaration to use the System.EventHandler<TEventArgs> delegate.
The following example shows a delegate that violates the rule. In the Visual Basic example, comments describe how to modify the example to satisfy the rule. For the C# example, an example follows that shows the modified code.
using System; namespace DesignLibrary { // This delegate violates the rule. public delegate void CustomEventHandler( object sender, CustomEventArgs e); public class CustomEventArgs : EventArgs { public string info = "data"; } public class ClassThatRaisesEvent { public event CustomEventHandler SomeEvent; protected virtual void OnSomeEvent(CustomEventArgs e) { if(SomeEvent != null) { SomeEvent(this, e); } } public void SimulateEvent() { OnSomeEvent(new CustomEventArgs()); } } public class ClassThatHandlesEvent { public ClassThatHandlesEvent(ClassThatRaisesEvent eventRaiser) { eventRaiser.SomeEvent += new CustomEventHandler(HandleEvent); } private void HandleEvent(object sender, CustomEventArgs e) { Console.WriteLine("Event handled: {0}", e.info); } } class Test { static void Main() { ClassThatRaisesEvent eventRaiser = new ClassThatRaisesEvent(); ClassThatHandlesEvent eventHandler = new ClassThatHandlesEvent(eventRaiser); eventRaiser.SimulateEvent(); } } }
The following example removes the delegate declaration from the previous example, which satisfies the rule, and replaces its use in the ClassThatRaisesEvent and ClassThatHandlesEvent methods by using the System.EventHandler<TEventArgs> delegate.
using System; namespace DesignLibrary { public class CustomEventArgs : EventArgs { public string info = "data"; } public class ClassThatRaisesEvent { public event EventHandler<CustomEventArgs> SomeEvent; protected virtual void OnSomeEvent(CustomEventArgs e) { if(SomeEvent != null) { SomeEvent(this, e); } } public void SimulateEvent() { OnSomeEvent(new CustomEventArgs()); } } public class ClassThatHandlesEvent { public ClassThatHandlesEvent(ClassThatRaisesEvent eventRaiser) { eventRaiser.SomeEvent += new EventHandler<CustomEventArgs>(HandleEvent); } private void HandleEvent(object sender, CustomEventArgs e) { Console.WriteLine("Event handled: {0}", e.info); } } class Test { static void Main() { ClassThatRaisesEvent eventRaiser = new ClassThatRaisesEvent(); ClassThatHandlesEvent eventHandler = new ClassThatHandlesEvent(eventRaiser); eventRaiser.SimulateEvent(); } } }
CA1005: Avoid excessive parameters on generic types
CA1010: Collections should implement generic interface
CA1000: Do not declare static members on generic types
CA1002: Do not expose generic lists
CA1006: Do not nest generic types in member signatures