ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – 在删除代理链之前是否有必要检查处理程序是否存在?

2019-07-14 18:06:32  阅读:113  来源: 互联网

标签:c event-handling delegates action


我有一个基于委托的消息传递系统的小片段,您可以订阅和取消订阅事件处理程序,并提升新事件.

在我的Unsubscribe方法中,我检查以确保处理程序首先存在,然后再删除它.

我的问题是:这项检查是否必要?即我不能这样做:

dic[type] -= handler;

无:

if (handler exists)
    dic[type] -= handler;

如果是这样,为什么? – 我尝试了检查而不是检查,它们都产生了相同的实际结果.不知道为什么我更愿意使用其中任何一个.

码:

    public abstract class GameEvent { }

    private static class EventManagerInternal<T> where T : GameEvent
    {
        private static Dictionary<Type, Action<T>> dic = new Dictionary<Type, Action<T>>();

        public static void Subscribe(Action<T> handler)
        {
            Type type = typeof(T);
            if (!dic.ContainsKey(type)) {
                dic[type] = handler;
                Console.WriteLine("Registered new type: " + type);
            }
            else {
                // make sure the handler doesn't exist first
                bool hasHandlerSubscribed = dic[type].GetInvocationList().Any(h => h.Equals(handler));
                if (hasHandlerSubscribed) {
                    Console.WriteLine(handler.Method.Name + " has already subbed to an event of type " + type);
                    return;
                }
                dic[type] += handler;
            }
            Console.WriteLine("Method " + handler.Method.Name + " has subbed to receive notifications from " + type);
        }

        public static void Unsubscribe(Action<T> handler)
        {
            Type type = typeof(T);

            // make sure the type exists
            if (!dic.ContainsKey(type)) {
                Console.WriteLine("Type " + type + " hasn't registered at all, it doesn't have any subscribers... at least not in my book...");
                return;
            }

            // get the methods that the delegate points to
            // to see if the handler exists or not
            bool exist = dic[type].GetInvocationList().Any(h => h.Equals(handler));
            if (!exist) {
                Console.WriteLine("Method " + handler.Method.Name + " hasn't registered at all, for notifications from " + type);
                return;
            }

            // remove the handler from the chain
            dic[type] -= handler;
            Console.WriteLine("handler " + handler.Method.Name + " has been removed. it won't take any notifications from " + type);

            // if there's no more subscribers to the "type" entry, remove it
            if (dic[type] == null) {
                dic.Remove(type);
                Console.WriteLine("No more subscribers to " + type);
            }
        }

        public static void Raise(T e)
        {
            Action<T> handler;
            if (dic.TryGetValue(e.GetType(), out handler)) {
                handler.Invoke(e);
            }
        }
    }

示例用法:(通过包装器 – 我只是认为Class.DoSomething< T>比Class< T> DoSomething更好 – 有一个很好的理由我为什么不得不去做这个设置:)不是这里的重点…… )

        Player p = new Player();
        EventManager.Subscribe<OnRename>(OnRenameHandler);
        EventManager.Subscribe<OnRename>(AnotherOnRenameHandler);
        EventManager.Raise(new OnRename());
        EventManager.Unsubscribe<OnRename>(OnRenameHandler);
        etc

解决方法:

My question is: is this check necessary?

不它不是.

why?

这很简单,检查由您正在使用的运算符完成.如果要删除的处理程序不存在,而不是抛出异常,它专门设计为什么都不做.

标签:c,event-handling,delegates,action
来源: https://codeday.me/bug/20190714/1459920.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有