C# 이벤트 디스패치

22897 단어 C#
이벤트 발송기는 감청하고자 하는 사건을 감청한 후에 유연하게 이벤트를 방송하여 촉발할 수 있고 코드를 더욱 조리화하고 읽기 쉽고 개발자의 규범에 부합되며 말을 많이 하지 않고 코드를 올릴 수 있다.
using System;
using System.Collections;
using System.Collections.Generic;

public class EventManager
{
    private static Dictionary<EventType, Delegate> EventDic = new Dictionary<EventType, Delegate>();

    #region     
    //none parameter
    public static void AddListenr(EventType eventType, DelegateCallBack delegateCallBack)
    {
        if (!EventDic.ContainsKey(eventType))
        {
            EventDic[eventType] = delegateCallBack;
        }
        else
        {
            EventDic[eventType] = (DelegateCallBack)EventDic[eventType] + delegateCallBack;
        }
    }

    //one parameter
    public static void AddListenr<T>(EventType eventType, DelegateCallBack<T> delegateCallBack)
    {
        if (!EventDic.ContainsKey(eventType))
        {
            EventDic[eventType] = delegateCallBack;
        }
        else
        {
            EventDic[eventType] = (DelegateCallBack<T>)EventDic[eventType] + delegateCallBack;
        }
    }

    //two parameter
    public static void AddListenr<T, X>(EventType eventType, DelegateCallBack<T,X> delegateCallBack)
    {
        if (!EventDic.ContainsKey(eventType))
        {
            EventDic[eventType] = delegateCallBack;
        }
        else
        {
            EventDic[eventType] = (DelegateCallBack<T,X>)EventDic[eventType] + delegateCallBack;
        }
    }

    #endregion

    #region     

    //none parameter
    public static void RemoveListener(EventType eventType, DelegateCallBack delegateCallBack)
    {
        if (EventDic.ContainsKey(eventType))
        {
            //           
            EventDic[eventType] = (DelegateCallBack)EventDic[eventType] - delegateCallBack;
        }
    }

    //one parameter
    public static void RemoveListener<T>(EventType eventType, DelegateCallBack<T> delegateCallBack)
    {
        if (EventDic.ContainsKey(eventType))
        {
            //           
            EventDic[eventType] = (DelegateCallBack<T>)EventDic[eventType] - delegateCallBack;
        }
    }

    //two parameter
    public static void RemoveListener<T,X>(EventType eventType, DelegateCallBack<T,X> delegateCallBack)
    {
        if (EventDic.ContainsKey(eventType))
        {
            //           
            EventDic[eventType] = (DelegateCallBack<T,X>)EventDic[eventType] - delegateCallBack;
        }
    }

    #endregion

    #region     

    //none parameter  
    public static void BrocastEvent(EventType eventType)
    {
        if (EventDic.ContainsKey(eventType))
        {
            DelegateCallBack delegateCallBack = (DelegateCallBack)EventDic[eventType];
            delegateCallBack();
        }
    }

    //one parameter
    public static void BrocastEvent<T>(EventType eventType,T arg1)
    {
        if (EventDic.ContainsKey(eventType))
        {
            DelegateCallBack<T> delegateCallBack = (DelegateCallBack<T>)EventDic[eventType];
            delegateCallBack(arg1);
        }
    }

    //two parameter
    public static void BrocastEvent<T,X>(EventType eventType, T arg1,X arg2)
    {
        if (EventDic.ContainsKey(eventType))
        {
            DelegateCallBack<T,X> delegateCallBack = (DelegateCallBack<T,X>)EventDic[eventType];
            delegateCallBack(arg1,arg2);
        }
    }

    #endregion
    
}


#region     
public delegate void DelegateCallBack();
public delegate void DelegateCallBack<T>(T arg1);
public delegate void DelegateCallBack<T,X>(T arg1,X arg2);
public delegate void DelegateCallBack<T,X,Y>(T arg1,X arg2,Y arg3);
public delegate void DelegateCallBack<T,X,Y,Z>(T arg1,X arg2,Y arg3,Z arg4);
#endregion


#region     
public enum EventType
{
    eventType1,
    eventType2,
    eventType3,
}

#endregion

좋은 웹페이지 즐겨찾기