zoukankan      html  css  js  c++  java
  • 最简洁的泛型c#消息机制

    using System;
    using System.Collections;

    public class EventSystem {
    public delegate void ListenerDelegate();
    public delegate void ListenerDelegate<T>(T t);
    public delegate void ListenerDelegate<T1, T2>(T1 t1, T2 t2);
    public delegate void ListenerDelegate<T1, T2, T3>(T1 t1, T2 t2, T3 t3);

    private Hashtable listeners = new Hashtable();

    #region Addlistener
    public void AddListener(string eventName, ListenerDelegate listener) {
    ListenerDelegate listenerDelegate = listeners[eventName] as ListenerDelegate;
    listenerDelegate = (ListenerDelegate)Delegate.Combine(listenerDelegate, listener);
    listeners[eventName] = listenerDelegate;
    }

    public void AddListener<T>(string eventName, ListenerDelegate<T> listener) {
    ListenerDelegate<T> listenerDelegate = listeners[eventName] as ListenerDelegate<T>;
    listenerDelegate = (ListenerDelegate<T>)Delegate.Combine(listenerDelegate, listener);
    listeners[eventName] = listenerDelegate;
    }

    public void AddListener<T1,T2>(string eventName, ListenerDelegate<T1,T2> listener) {
    ListenerDelegate<T1,T2> listenerDelegate = listeners[eventName] as ListenerDelegate<T1,T2>;
    listenerDelegate = (ListenerDelegate<T1,T2>)Delegate.Combine(listenerDelegate, listener);
    listeners[eventName] = listenerDelegate;
    }

    public void AddListener<T1,T2,T3>(string eventName, ListenerDelegate<T1,T2,T3> listener) {
    ListenerDelegate<T1,T2,T3> listenerDelegate = listeners[eventName] as ListenerDelegate<T1,T2,T3>;
    listenerDelegate = (ListenerDelegate<T1,T2,T3>)Delegate.Combine(listenerDelegate, listener);
    listeners[eventName] = listenerDelegate;
    }
    #endregion

    #region RemoveListener
    public void RemoveListener(string eventName, ListenerDelegate listener) {
    Delegate remove = Delegate.Remove(listeners[eventName] as ListenerDelegate, listener);
    SetRemoveValue(eventName, remove);
    }

    public void RemoveListener<T>(string eventName, ListenerDelegate<T> listener) {
    var remove = Delegate.Remove(listeners[eventName] as ListenerDelegate<T>, listener);
    SetRemoveValue(eventName, remove);
    }

    public void RemoveListener<T1, T2>(string eventName, ListenerDelegate<T1, T2> listener) {
    Delegate remove = Delegate.Remove(listeners[eventName] as ListenerDelegate<T1, T2>, listener);
    SetRemoveValue(eventName, remove);
    }

    public void RemoveListener<T1, T2, T3>(string eventName, ListenerDelegate<T1, T2, T3> listener) {
    Delegate remove = Delegate.Remove(listeners[eventName] as ListenerDelegate<T1, T2, T3>, listener);
    SetRemoveValue(eventName, remove);
    }

    private void SetRemoveValue(string eventName, Delegate remove) {
    if (remove == null) {
    listeners.Remove(eventName);
    } else {
    listeners[eventName] = remove;
    }
    }
    #endregion

    public void RemoveAllListener(string eventName) {
    listeners.Remove(eventName);
    }

    public void Dispatch(string eventName)
    {
    if (listeners.ContainsKey(eventName)) {
    ListenerDelegate listenerDelegate = listeners[eventName] as ListenerDelegate;
    listenerDelegate();
    }
    }

    public void Dispatch<T>(string eventName, T t) {
    if (listeners.ContainsKey(eventName)) {
    ListenerDelegate<T> listenerDelegate = listeners[eventName] as ListenerDelegate<T>;
    listenerDelegate(t);
    }
    }

    public void Dispatch<T1, T2>(string eventName, T1 t1, T2 t2) {
    if (listeners.ContainsKey(eventName)) {
    ListenerDelegate<T1,T2> listenerDelegate = listeners[eventName] as ListenerDelegate<T1,T2>;
    listenerDelegate(t1, t2);
    }
    }

    public void Dispatch<T1, T2, T3>(string eventName, T1 t1, T2 t2, T3 t3) {
    if (listeners.ContainsKey(eventName)) {
    ListenerDelegate<T1,T2,T3> listenerDelegate = listeners[eventName] as ListenerDelegate<T1,T2,T3>;
    listenerDelegate(t1, t2, t3);
    }
    }
    }

    最多支持3个参数,再多我认为要写结构体了

    Delegate.Combine和+= 是有区别的   https://www.coder.work/article/1627775

  • 相关阅读:
    python excel 像 Excel 一样使用 python 进行数据分析
    ubuntu 系统分区
    ubuntu 配置和修改ip地址
    yum centos 修改镜像源
    centos6 安装 docker 问题
    Feign 的简单使用(2)
    feign client 的简单使用(1)
    xsync
    canal 配置
    javascript之DOM编程正则表达式引入
  • 原文地址:https://www.cnblogs.com/sanyejun/p/15622974.html
Copyright © 2011-2022 走看看