1. 程式人生 > 程式設計 >C#事件管理器如何清空所有監聽詳解

C#事件管理器如何清空所有監聽詳解

C#事件使用+= -=使用起來是很方便的,但是卻不能整體清空所有事件。比如一個常見的操作,開啟介面註冊監聽事件,關閉介面需要把所有的事件清空了,這要在寫一堆-=操作,如果漏清空的話肯定會造成隱患,如果在lua裡這個很容易,但是C#卻不行。所以我想了個辦法,對Action和Func進行一次包裝,就可以解決這個問題了。

這裡我只封裝了兩個引數,大家可以繼續拓展新的引數,我在專案裡一共拓展了5個引數,完全夠用了。

using System;
using System.Collections.Generic;
 
public class ActionManager
{
  Dictionary<object,object> m_Actions = new Dictionary<object,object>();
 
  public NewAction RegAction(NewAction newAction,Action action)
  {
    newAction += action;
    m_Actions[newAction] = action;
    return newAction;
  }
  public NewAction<T1> RegAction<T1>(NewAction<T1> newAction,Action<T1> action)
  {
    newAction += action;
    m_Actions[newAction] = action;
    return newAction;
  }
  public NewFunc<T1> RegAction<T1>(NewFunc<T1> newAction,Func<T1> action)
  {
    newAction += action;
    m_Actions[newAction] = action;
    return newAction;
  }
  public NewFunc<T1,T2> RegAction<T1,T2>(NewFunc<T1,T2> newAction,Func<T1,T2> action)
  {
    newAction += action;
    m_Actions[newAction] = action;
    return newAction;
  }
 
  public void Clear()
  {
    foreach (var act in m_Actions)
    {
      ((IAction)act.Key).Dispose(act.Value);
    }
  }
}
 
public interface IAction
{
  void Dispose(object obj);
}
public class NewAction : IAction
{
  Action action;
  public void Dispose(object obj)
  {
    if(obj is Action act)
      action -= act;
  }
  public void Invoke()
  {
    action?.Invoke();
  }
  public static NewAction operator +(NewAction a,Action b)
  {
    a.action -= b;
    a.action += b;
    return a;
  }
  public static NewAction operator -(NewAction a,Action b)
  {
    a.action -= b;
    return a;
  }
}
public class NewAction<T1> : IAction
{
  Action<T1> action;
  public void Dispose(object obj)
  {
    if (obj is Action<T1> act)
      action -= act;
  }
  public void Invoke(T1 t1)
  {
    action?.Invoke(t1);
  }
  public static NewAction<T1> operator +(NewAction<T1> a,Action<T1> b)
  {
    a.action -= b;
    a.action += b;
    return a;
  }
  public static NewAction<T1> operator -(NewAction<T1> a,Action<T1> b)
  {
    a.action -= b;
    return a;
  }
}
public class NewFunc<T1> : IAction
{
  Func<T1> func;
  public void Dispose(object obj)
  {
    if (obj is Func<T1> act)
      func -= act;
  }
  public T1 Invoke()
  {
    return func != null ? func.Invoke() : default(T1);
  }
  public static NewFunc<T1> operator +(NewFunc<T1> a,Func<T1> b)
  {
    a.func -= b;
    a.func += b;
    return a;
  }
  public static NewFunc<T1> operator -(NewFunc<T1> a,Func<T1> b)
  {
    a.func -= b;
    return a;
  }
}
public class NewFunc<T1,T2> : IAction
{
  Func<T1,T2> func;
  public void Dispose(object obj)
  {
    if (obj is Func<T1,T2> act)
      func -= act;
  }
  public T2 Invoke(T1 t1)
  {
    return func != null ? func.Invoke(t1) : default(T2);
  }
  public static NewFunc<T1,T2> operator +(NewFunc<T1,T2> a,T2> b)
  {
    a.func -= b;
    a.func += b;
    return a;
  }
  public static NewFunc<T1,T2> operator -(NewFunc<T1,T2> b)
  {
    a.func -= b;
    return a;
  }
}

使用方法如下,注意我們自己封裝的事件必須要new。

using UnityEngine;
 
public class Main : MonoBehaviour
{
  NewAction<string> MyAction = new NewAction<string>();//事件需要new
  NewFunc<string,int> MyFunc = new NewFunc<string,int>();//事件需要new
 
 
  ActionManager m_ActionManager = new ActionManager();
 
  public void MyFunction(string str)
  {
    Debug.Log(" MyFunction " + str);
  }
  public int MyFunction1(string str)
  {
    Debug.Log(" MyFunction1 " + str);
    return 1;
  }
 
  private void OnGUI()
  {
    if (GUILayout.Button("<size=50>註冊事件</size>"))
    {
      m_ActionManager.RegAction(MyAction,MyFunction);
      m_ActionManager.RegAction(MyFunc,MyFunction1);
    }
 
    if (GUILayout.Button("<size=50>發事件</size>"))
    {
      MyAction.Invoke("引數1");
      MyFunc.Invoke("引數2");
    }
    if (GUILayout.Button("<size=50>清空</size>"))
    {
      m_ActionManager.Clear();
    }
  }
}

事件管理器可以放在UI或者模組的基類中,這樣子類在寫的時候可以直接this.RegAction註冊事件,關閉介面或者解除安裝模組的時候由父類呼叫Clear方法,這樣業務邏輯就不需要在寫-=這樣的程式碼了。

總結

到此這篇關於C#事件管理器如何清空所有監聽的文章就介紹到這了,更多相關C#事件管理器清空所有監聽內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!