/
EventHandlerExtensions.cs
62 lines (55 loc) · 2.49 KB
/
EventHandlerExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/********************************************************
* *
* © Copyright (C) Microsoft. All rights reserved. *
* *
*********************************************************/
namespace Microsoft
{
using System;
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// Extension methods to make it easier to safely invoke events.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate", Justification = "These aren't events.")]
public static class EventHandlerExtensions
{
/// <summary>
/// Invokes any event handlers that are hooked to the specified event.
/// </summary>
/// <param name="handler">The event. Null is allowed.</param>
/// <param name="sender">The value to pass as the sender of the event.</param>
/// <param name="e">Event arguments to include.</param>
public static void Raise(this Delegate handler, object sender, EventArgs e)
{
Requires.NotNull(sender, nameof(sender));
Requires.NotNull(e, nameof(e));
handler?.DynamicInvoke(sender, e);
}
/// <summary>
/// Invokes any event handlers that are hooked to the specified event.
/// </summary>
/// <param name="handler">The event. Null is allowed.</param>
/// <param name="sender">The value to pass as the sender of the event.</param>
/// <param name="e">Event arguments to include.</param>
public static void Raise(this EventHandler handler, object sender, EventArgs e)
{
Requires.NotNull(sender, nameof(sender));
Requires.NotNull(e, nameof(e));
handler?.Invoke(sender, e);
}
/// <summary>
/// Invokes any event handlers that are hooked to the specified event.
/// </summary>
/// <typeparam name="T">The type of EventArgs.</typeparam>
/// <param name="handler">The event. Null is allowed.</param>
/// <param name="sender">The value to pass as the sender of the event.</param>
/// <param name="e">Event arguments to include.</param>
public static void Raise<T>(this EventHandler<T> handler, object sender, T e)
where T : EventArgs
{
Requires.NotNull(sender, nameof(sender));
Requires.NotNull(e, nameof(e));
handler?.Invoke(sender, e);
}
}
}