-
Notifications
You must be signed in to change notification settings - Fork 20
/
Extensions.cs
51 lines (42 loc) · 1.37 KB
/
Extensions.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
using System;
using System.Threading.Tasks;
namespace fs2ff
{
public static class Extensions
{
public static T AdjustToBounds<T>(this T value, T min, T max) where T : IComparable =>
value.CompareTo(min) < 0
? min
: value.CompareTo(max) > 0
? max
: value;
public static async Task RaiseAsync<T>(this Func<T, Task>? handler, T value)
{
if (handler == null)
{
return;
}
Delegate[] delegates = handler.GetInvocationList();
Task[] tasks = new Task[delegates.Length];
for (var i = 0; i < delegates.Length; i++)
{
tasks[i] = ((Func<T, Task>) delegates[i])(value);
}
await Task.WhenAll(tasks);
}
public static async Task RaiseAsync<T1, T2>(this Func<T1, T2, Task>? handler, T1 value1, T2 value2)
{
if (handler == null)
{
return;
}
Delegate[] delegates = handler.GetInvocationList();
Task[] tasks = new Task[delegates.Length];
for (var i = 0; i < delegates.Length; i++)
{
tasks[i] = ((Func<T1, T2, Task>) delegates[i])(value1, value2);
}
await Task.WhenAll(tasks);
}
}
}