C#事件管理器如何清空所有监听详解
程序员文章站
2022-11-16 10:55: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方法,这样业务逻辑就不需要在写-=这样的代码了。
总结
到此这篇关于c#事件管理器如何清空所有监听的文章就介绍到这了,更多相关c#事件管理器清空所有监听内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!