您的位置:首页 > 汽车 > 新车 > 建一个com网站要多少钱_微信网站建设方案_淘宝关键词搜索量查询_属于seo网站优化

建一个com网站要多少钱_微信网站建设方案_淘宝关键词搜索量查询_属于seo网站优化

2025/4/29 19:59:52 来源:https://blog.csdn.net/weixin_44238530/article/details/147348733  浏览:    关键词:建一个com网站要多少钱_微信网站建设方案_淘宝关键词搜索量查询_属于seo网站优化
建一个com网站要多少钱_微信网站建设方案_淘宝关键词搜索量查询_属于seo网站优化

之前写了一个简单的事件工具类,使用字典来进行存储。
当时想的是使用TryGetValue少进行一次遍历。在项目使用中发现只有一个事件生效。后续在注册的事件无法生效。

当时的写法

        private readonly Dictionary<int, Action<IEventMessage>> mListeners;/// <summary>/// 注册事件/// </summary>public IUnRegister Register<TEvent>(Action<IEventMessage> listener) where TEvent : IEventMessage{int eventId = typeof(TEvent).GetHashCode();if (mListeners.TryGetValue(eventId,out var listeners)){listeners += listener;}else{mListeners.Add(eventId, listener);}return new CustomUnRegister(() => UnRegister<TEvent>(listener));}

更改后的写法

        private readonly Dictionary<int, Action<IEventMessage>> mListeners;/// <summary>/// 注册事件/// </summary>public IUnRegister Register<TEvent>(Action<IEventMessage> listener) where TEvent : IEventMessage{int eventId = typeof(TEvent).GetHashCode();if (mListeners.ContainsKey(eventId)){mListeners[eventId] += listener;}else{mListeners.Add(eventId, listener);}return new CustomUnRegister(() => UnRegister<TEvent>(listener));}

deepseek的回答:
在这里插入图片描述
可以使用下面代码验证上述回答

        Action action = () =>{Debug.Log("action");};Action action2 = action;action += () =>{Debug.Log("action +=");};action.Invoke();Debug.Log("-------------------------------------------");action2.Invoke();

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com