CA1003:必須使用一般事件處理常式執行個體
型別名稱 |
UseGenericEventHandlerInstances |
CheckId |
CA1003 |
分類 |
Microsoft.Design |
中斷變更 |
中斷 |
原因
型別包含會傳回 void 的委派 (Delegate),其簽章 (Signature) 包含兩個參數 (第一個參數是物件,第二個參數則是可指派給 EventArgs 的型別),而包含組件 (Assembly) 則會以 .NET Framework 2.0 為目標。
規則描述
在 .NET Framework 2.0 之前,為了將自訂資訊傳遞給事件處理常式,必須宣告新的委派,指定衍生自 System.EventArgs 類別的類別。 在 .NET Framework 2.0 中,引入 System.EventHandler<TEventArgs> 委派不再是必要的。 這個泛型委派允許所有衍生自 EventArgs 的類別都可以與事件處理常式一起使用。
如何修正違規
若要修正此規則的違規情形,請移除此委派,並改用 System.EventHandler<TEventArgs> 委派。 如果此委派是由 Visual Basic 編譯器自動產生,請變更事件宣告的語法,以使用 System.EventHandler<TEventArgs> 委派。
隱藏警告的時機
請勿隱藏此規則的警告。
範例
下列範例會顯示違反此規則的委派。 在 Visual Basic 的範例中,註解會描述如何修改範例以滿足規則。 以下為 C# 的範例,會顯示修改過的程式碼。
Imports System
Namespace DesignLibrary
Public Class CustomEventArgs
Inherits EventArgs
Public info As String = "data"
End Class
Public Class ClassThatRaisesEvent
' This statement creates a new delegate, which violates the rule.
Event SomeEvent(sender As Object, e As CustomEventArgs)
' To satisfy the rule, comment out the previous line
' and uncomment the following line.
'Event SomeEvent As EventHandler(Of CustomEventArgs)
Protected Overridable Sub OnSomeEvent(e As CustomEventArgs)
RaiseEvent SomeEvent(Me, e)
End Sub
Sub SimulateEvent()
OnSomeEvent(New CustomEventArgs())
End Sub
End Class
Public Class ClassThatHandlesEvent
Sub New(eventRaiser As ClassThatRaisesEvent)
AddHandler eventRaiser.SomeEvent, AddressOf HandleEvent
End Sub
Private Sub HandleEvent(sender As Object, e As CustomEventArgs)
Console.WriteLine("Event handled: {0}", e.info)
End Sub
End Class
Class Test
Shared Sub Main()
Dim eventRaiser As New ClassThatRaisesEvent()
Dim eventHandler As New ClassThatHandlesEvent(eventRaiser)
eventRaiser.SimulateEvent()
End Sub
End Class
End Namespace
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();
}
}
}
下列範例會從前一個範例中移除委派宣告以滿足規則,並在 ClassThatRaisesEvent 和 ClassThatHandlesEvent 方法中改用 System.EventHandler<TEventArgs> 委派。
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();
}
}
}