C#事件管理器怎样清空所有监听详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C#事件管理器怎样清空所有监听详解

雨松MOMO   2020-07-17 我要评论

本文着重给大家讲解了关于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, Func<T1, T2> b)
  {
    a.func -= b;
    a.func += b;
    return a;
  }
  public static NewFunc<T1, T2> operator -(NewFunc<T1, T2> a, Func<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方法,这样业务逻辑就不需要在写-=这样的代码了。

总结

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们