• 最简洁的泛型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

  • 相关阅读:
    第二个冲刺
    实验四主存空间的分配和回收
    Sprint总结
    Sprint回顾
    Scrum项目5.0
    hibernate jpa 注解 @Temporal(TemporalType.DATE) 格式化时间日期,页面直接得到格式化类型的值
    获得HttpServletResponse及其他对象
    UUID.randomUUID()方法介绍
    psp个人软件过程需求文档
    电子产品自动搜索比价系统设计与实现 项目愿景与范围
  • 原文地址:https://www.cnblogs.com/sanyejun/p/15622974.html
Copyright © 2020-2023  润新知