Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class CustomEvent{
  5.     public int Count{
  6.         get {
  7.             return events.Count;
  8.         }
  9.     }
  10.  
  11.     private readonly List<Action> events;
  12.  
  13.     public CustomEvent(int capacity = 1){
  14.         events = new List<Action>(capacity);
  15.     }
  16.  
  17.     public event Action Event{
  18.         add{
  19.             events.Add(value);
  20.         }
  21.  
  22.         remove{
  23.             events.Remove(value);
  24.         }
  25.     }
  26.  
  27.     public void AddEvent(Action Event){
  28.         events.Add(Event);
  29.     }
  30.  
  31.     public void RemoveEvent(Action Event){
  32.         events.Remove(Event);
  33.     }
  34.  
  35.     public void Execute(){
  36.         for(var i = 0; i < events.Count; ++i) {
  37.             if(events[i] != null) {
  38.                 events[i]();
  39.             }
  40.         }
  41.     }
  42.  
  43.     public void ExecuteAndKill(){
  44.         for(var i = 0; i < events.Count; ++i) {
  45.             if(events[i] != null){
  46.                 events[i]();
  47.                 events.Remove(events[i]);
  48.             }
  49.         }
  50.     }
  51.  
  52.     public void Clear(){
  53.         events.Clear();
  54.     }
  55.  
  56. }
  57.  
  58. public class CustomEvent<T>{
  59.     public int Count{
  60.         get{
  61.             return events.Count;
  62.         }
  63.     }
  64.  
  65.     private readonly List<Action<T>> events;
  66.  
  67.     public CustomEvent(int capacity = 1) {
  68.         events = new List<Action<T>>(capacity);
  69.     }
  70.  
  71.     public event Action<T> Event{
  72.         add{
  73.             events.Add(value);
  74.         }
  75.  
  76.         remove{
  77.             events.Remove(value);
  78.         }
  79.     }
  80.  
  81.     public void Execute(T value){
  82.         for(var i = 0; i < events.Count; ++i){
  83.             if(events[i] != null){
  84.                 events[i](value);
  85.             }
  86.         }
  87.     }
  88.  
  89.     public void Clear(){
  90.         events.Clear();
  91.     }
  92. }
  93.  
  94. public class CustomEvent<A, B>{
  95.     private readonly List<Action<A, B>> events;
  96.  
  97.     public CustomEvent(int capacity = 1){
  98.         events = new List<Action<A, B>>(capacity);
  99.     }
  100.  
  101.     public event Action<A, B> Event{
  102.         add{
  103.             events.Add(value);
  104.         }
  105.  
  106.         remove{
  107.             events.Remove(value);
  108.         }
  109.     }
  110.  
  111.     public void Execute(A AValue, B BValue){
  112.         for(int i = 0; i < events.Count; i++){
  113.             if(events[i] != null){
  114.                 events[i](AValue, BValue);
  115.             }
  116.         }
  117.     }
  118.  
  119.     public void Clear(){
  120.         events.Clear();
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement