Skip to content

Commit d094193

Browse files
committed
Create common Event
Used to streamline usage of c# events with public Invoke and Subscribe methods #164
1 parent 1634a13 commit d094193

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/v4/Common/Common/Events/Event.cs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Common.Exceptions;
2+
3+
namespace Common.Events;
4+
5+
public class Event
6+
{
7+
private event EventHandler? GenericEvent;
8+
9+
public void Invoke(object sender)
10+
{
11+
if (sender == null)
12+
{
13+
throw new CommonException("Invalid invocation attempt - sender is null");
14+
}
15+
GenericEvent?.Invoke(sender, EventArgs.Empty);
16+
}
17+
public void Subscrive(Action<object> action)
18+
{
19+
GenericEvent += (sender, _) => action(sender!);
20+
}
21+
}
22+
23+
public class Event<T>
24+
where T : class
25+
{
26+
private event EventHandler<T>? GenericEvent;
27+
28+
public void Invoke(object sender, T data)
29+
{
30+
if (sender == null)
31+
{
32+
throw new CommonException("Invalid invocation attempt - sender is null");
33+
}
34+
GenericEvent?.Invoke(sender, data);
35+
}
36+
public void Subscrive(Action<object, T> action)
37+
{
38+
GenericEvent += (sender, data) => action(sender!, data);
39+
}
40+
}
41+
42+
public class Event<T1, T2>
43+
where T1 : class
44+
where T2 : class
45+
{
46+
private event EventHandler<(T1, T2)>? GenericEvent;
47+
48+
public void Invoke(object sender, T1 data1, T2 data2)
49+
{
50+
if (sender == null)
51+
{
52+
throw new CommonException("Invalid invocation attempt - sender is null");
53+
}
54+
GenericEvent?.Invoke(sender, (data1, data2));
55+
}
56+
public void Subscrive(Action<object, T1, T2> action)
57+
{
58+
GenericEvent += (sender, data) => action(sender!, data.Item1, data.Item2);
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Common.Exceptions;
2+
3+
public class CommonException : Exception
4+
{
5+
public CommonException(string message) : base(message)
6+
{
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace Common.Utilities;
2+
3+
public static class ReflectionUtilities
4+
{
5+
}

0 commit comments

Comments
 (0)