2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2020-06-30 12:48:39 +02:00
|
|
|
Delegate event handlers (i.e. delegates used as type of an event) should have a very specific signature:
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2021-01-27 13:42:22 +01:00
|
|
|
* Return type ``++void++``.
|
|
|
|
* First argument of type ``++System.Object++`` and named 'sender'.
|
|
|
|
* Second argument of type ``++System.EventArgs++`` (or any derived type) and is named 'e'.
|
2020-06-30 12:48:39 +02:00
|
|
|
|
2021-01-27 13:42:22 +01:00
|
|
|
This rule raises an issue whenever a ``++delegate++`` declaration doesn't match that signature.
|
2020-06-30 12:48:39 +02:00
|
|
|
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2020-06-30 12:48:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,text]
|
2020-06-30 12:48:39 +02:00
|
|
|
----
|
|
|
|
public delegate void AlarmEventHandler(object s);
|
|
|
|
|
|
|
|
public class Foo
|
|
|
|
{
|
|
|
|
public event AlarmEventHandler AlarmEvent; // Noncompliant
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2020-06-30 12:48:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,text]
|
2020-06-30 12:48:39 +02:00
|
|
|
----
|
|
|
|
public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
|
|
|
|
|
|
|
|
public class Foo
|
|
|
|
{
|
|
|
|
public event AlarmEventHandler AlarmEvent; // Compliant
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
== Resources
|
2020-06-30 12:48:39 +02:00
|
|
|
|
|
|
|
https://msdn.microsoft.com/en-us/library/edzehd2t.aspx[Handling and Raising Events]
|
|
|
|
|