95 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
namespace Syntriax.Engine.Core;
public class Event
{
private readonly List<Action> listeners = new(1000);
public void AddListener(Action listener) => listeners.Add(listener);
public void RemoveListener(Action listener) => listeners.Remove(listener);
public void Invoke()
{
for (int i = 0; i < listeners.Count; i++)
try { listeners[i].Invoke(); }
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Method.DeclaringType?.FullName}.{listeners[i].Method.Name}()";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
}
}
public class Event<T1>
{
private readonly List<Action<T1>> listeners = new(1000);
public void AddListener(Action<T1> listener) => listeners.Add(listener);
public void RemoveListener(Action<T1> listener) => listeners.Remove(listener);
public void Invoke(T1 argument)
{
for (int i = 0; i < listeners.Count; i++)
try { listeners[i].Invoke(argument); }
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Method.DeclaringType?.FullName}.{listeners[i].Method.Name}({argument})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
}
}
public class Event<T1, T2>
{
private readonly List<Action<T1, T2>> listeners = new(1000);
public void AddListener(Action<T1, T2> listener) => listeners.Add(listener);
public void RemoveListener(Action<T1, T2> listener) => listeners.Remove(listener);
public void Invoke(T1 argument1, T2 argument2)
{
for (int i = 0; i < listeners.Count; i++)
try { listeners[i].Invoke(argument1, argument2); }
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Method.DeclaringType?.FullName}.{listeners[i].Method.Name}({string.Join(", ", argument1, argument2)})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
}
}
public class Event<T1, T2, T3>
{
private readonly List<Action<T1, T2, T3>> listeners = new(1000);
public void AddListener(Action<T1, T2, T3> listener) => listeners.Add(listener);
public void RemoveListener(Action<T1, T2, T3> listener) => listeners.Remove(listener);
public void Invoke(T1 argument1, T2 argument2, T3 argument3)
{
for (int i = 0; i < listeners.Count; i++)
try { listeners[i].Invoke(argument1, argument2, argument3); }
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Method.DeclaringType?.FullName}.{listeners[i].Method.Name}({string.Join(", ", argument1, argument2, argument3)})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
}
}
public class Event<T1, T2, T3, T4>
{
private readonly List<Action<T1, T2, T3, T4>> listeners = new(1000);
public void AddListener(Action<T1, T2, T3, T4> listener) => listeners.Add(listener);
public void RemoveListener(Action<T1, T2, T3, T4> listener) => listeners.Remove(listener);
public void Invoke(T1 argument1, T2 argument2, T3 argument3, T4 argument4)
{
for (int i = 0; i < listeners.Count; i++)
try { listeners[i].Invoke(argument1, argument2, argument3, argument4); }
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Method.DeclaringType?.FullName}.{listeners[i].Method.Name}({string.Join(", ", argument1, argument2, argument3, argument4)})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
}
}