{"id":91,"date":"2019-10-14T19:07:43","date_gmt":"2019-10-14T11:07:43","guid":{"rendered":"http:\/\/www.blinkedu.cn\/?p=91"},"modified":"2019-10-14T19:10:09","modified_gmt":"2019-10-14T11:10:09","slug":"c%ef%bc%9a%e5%9f%ba%e4%ba%8e%e6%9e%9a%e4%b8%be%e7%9a%84%e7%ae%80%e6%98%93%e4%ba%8b%e4%bb%b6%e7%9b%91%e5%90%ac%e4%b8%8e%e5%b9%bf%e6%92%ad%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/www.blinkedu.cn\/index.php\/2019\/10\/14\/c%ef%bc%9a%e5%9f%ba%e4%ba%8e%e6%9e%9a%e4%b8%be%e7%9a%84%e7%ae%80%e6%98%93%e4%ba%8b%e4%bb%b6%e7%9b%91%e5%90%ac%e4%b8%8e%e5%b9%bf%e6%92%ad%e7%b3%bb%e7%bb%9f\/","title":{"rendered":"C#\uff1a\u57fa\u4e8e\u679a\u4e3e\u7684\u7b80\u6613\u4e8b\u4ef6\u76d1\u542c\u4e0e\u5e7f\u64ad\u7cfb\u7edf"},"content":{"rendered":"<h2>\u4ee3\u7801<\/h2>\n<ul>\n<li><strong>\u59d4\u6258\u5b9a\u4e49<\/strong><\/li>\n<\/ul>\n<pre><code class=\"language-csharp line-numbers\">namespace EventSystem\n{\n    public delegate void Callback();\n\n    public delegate void Callback&lt;T&gt;(T arg);\n\n    public delegate void Callback&lt;T, X&gt;(T arg1, X arg2);\n\n    public delegate void Callback&lt;T, X, Y&gt;(T arg1, X arg2, Y arg3);\n\n    public delegate void Callback&lt;T, X, Y, Z&gt;(T arg1, X arg2,Y arg3,Z arg4);\n\n    public delegate void Callback&lt;T, X, Y, Z, W&gt;(T arg1, X arg2,Y arg3, Z arg4, W arg5);\n}\n<\/code><\/pre>\n<ul>\n<li><strong>\u6d88\u606f\u7c7b\u578b<\/strong><\/li>\n<\/ul>\n<pre><code class=\"language-csharp line-numbers\">namespace EventSystem\n{\n    public enum EventType\n    {\n        Update\n    }\n}\n<\/code><\/pre>\n<ul>\n<li><strong>\u6d88\u606f\u4e2d\u5fc3(\u6838\u5fc3)<\/strong><\/li>\n<\/ul>\n<pre><code class=\"language-csharp line-numbers\">using System;\nusing System.Collections.Generic;\n\nnamespace EventSystem\n{\n    public class EventCenter\n    {\n        private static readonly Dictionary&lt;EventType, Delegate&gt; m_EventMap = new Dictionary&lt;EventType, Delegate&gt;();\n\n        #region \u6dfb\u52a0\u76d1\u542c\n\n        public static void AddListener(EventType type, Callback callback)\n        {\n            DoAddListenerCheck(type, callback);\n            m_EventMap[type] = (m_EventMap[type] as Callback) + callback;\n        }\n\n        public static void AddListener&lt;T&gt;(EventType type, Callback&lt;T&gt; callback)\n        {\n            DoAddListenerCheck(type, callback);\n            m_EventMap[type] = (m_EventMap[type] as Callback&lt;T&gt;) + callback;\n        }\n\n        public static void AddListener&lt;T, X&gt;(EventType type, Callback&lt;T, X&gt; callback)\n        {\n            DoAddListenerCheck(type, callback);\n            m_EventMap[type] = (m_EventMap[type] as Callback&lt;T, X&gt;) + callback;\n        }\n\n        public static void AddListener&lt;T, X, Y&gt;(EventType type, Callback&lt;T, X, Y&gt; callback)\n        {\n            DoAddListenerCheck(type, callback);\n            m_EventMap[type] = (m_EventMap[type] as Callback&lt;T, X, Y&gt;) + callback;\n        }\n\n        public static void AddListener&lt;T, X, Y, Z&gt;(EventType type, Callback&lt;T, X, Y, Z&gt; callback)\n        {\n            DoAddListenerCheck(type, callback);\n            m_EventMap[type] = (m_EventMap[type] as Callback&lt;T, X, Y, Z&gt;) + callback;\n        }\n\n        public static void AddListener&lt;T, X, Y, Z, W&gt;(EventType type, Callback&lt;T, X, Y, Z, W&gt; callback)\n        {\n            DoAddListenerCheck(type, callback);\n            m_EventMap[type] = (m_EventMap[type] as Callback&lt;T, X, Y, Z, W&gt;) + callback;\n        }\n\n        #endregion\n\n        #region \u79fb\u9664\u76d1\u542c\n\n        public static void RemoveListener(EventType type, Callback callback)\n        {\n            DoRemoveListenerCheck(type, callback);\n            m_EventMap[type] = (m_EventMap[type] as Callback) - callback;\n            if (m_EventMap[type] == null) m_EventMap.Remove(type);\n        }\n\n        public static void RemoveListener&lt;T&gt;(EventType type, Callback&lt;T&gt; callback)\n        {\n            DoRemoveListenerCheck(type, callback);\n            m_EventMap[type] = (m_EventMap[type] as Callback&lt;T&gt;) - callback;\n            if (m_EventMap[type] == null) m_EventMap.Remove(type);\n        }\n\n        public static void RemoveListener&lt;T, X&gt;(EventType type, Callback&lt;T, X&gt; callback)\n        {\n            DoRemoveListenerCheck(type, callback);\n            m_EventMap[type] = (m_EventMap[type] as Callback&lt;T, X&gt;) - callback;\n            if (m_EventMap[type] == null) m_EventMap.Remove(type);\n        }\n\n        public static void RemoveListener&lt;T, X, Y&gt;(EventType type, Callback&lt;T, X, Y&gt; callback)\n        {\n            DoRemoveListenerCheck(type, callback);\n            m_EventMap[type] = (m_EventMap[type] as Callback&lt;T, X, Y&gt;) - callback;\n            if (m_EventMap[type] == null) m_EventMap.Remove(type);\n        }\n\n        public static void RemoveListener&lt;T, X, Y, Z&gt;(EventType type, Callback&lt;T, X, Y, Z&gt; callback)\n        {\n            DoRemoveListenerCheck(type, callback);\n            m_EventMap[type] = (m_EventMap[type] as Callback&lt;T, X, Y, Z&gt;) - callback;\n            if (m_EventMap[type] == null) m_EventMap.Remove(type);\n        }\n\n        public static void RemoveListener&lt;T, X, Y, Z, W&gt;(EventType type, Callback&lt;T, X, Y, Z, W&gt; callback)\n        {\n            DoRemoveListenerCheck(type, callback);\n            m_EventMap[type] = (m_EventMap[type] as Callback&lt;T, X, Y, Z, W&gt;) - callback;\n            if (m_EventMap[type] == null) m_EventMap.Remove(type);\n        }\n\n        #endregion\n\n        #region \u5e7f\u64ad\n\n        public static void Broadcast(EventType type)\n        {\n            Delegate del = null;\n            if (m_EventMap.TryGetValue(type, out del))\n            {\n                Callback callback = (del as Callback);\n                if (callback != null)\n                    callback();\n            }\n            else\n                throw new Exception(&quot;\u4e8b\u4ef6\u5e7f\u64ad\u9519\u8bef!EventType=&quot; + type);\n        }\n\n        public static void Broadcast&lt;T&gt;(EventType type, T arg)\n        {\n            Delegate del = null;\n            if (m_EventMap.TryGetValue(type, out del))\n            {\n                Callback&lt;T&gt; callback = (del as Callback&lt;T&gt;);\n                if (callback != null)\n                    callback(arg);\n            }\n            else\n                throw new Exception(&quot;\u4e8b\u4ef6\u5e7f\u64ad\u9519\u8bef!EventType=&quot; + type);\n        }\n\n        public static void Broadcast&lt;T, X&gt;(EventType type, T arg1, X arg2)\n        {\n            Delegate del = null;\n            if (m_EventMap.TryGetValue(type, out del))\n            {\n                Callback&lt;T, X&gt; callback = (del as Callback&lt;T, X&gt;);\n                if (callback != null)\n                    callback(arg1, arg2);\n            }\n            else\n                throw new Exception(&quot;\u4e8b\u4ef6\u5e7f\u64ad\u9519\u8bef!EventType=&quot; + type);\n        }\n\n        public static void Broadcast&lt;T, X, Y&gt;(EventType type, T arg1, X arg2, Y arg3)\n        {\n            Delegate del = null;\n            if (m_EventMap.TryGetValue(type, out del))\n            {\n                Callback&lt;T, X, Y&gt; callback = (del as Callback&lt;T, X, Y&gt;);\n                if (callback != null)\n                    callback(arg1, arg2, arg3);\n            }\n            else\n                throw new Exception(&quot;\u4e8b\u4ef6\u5e7f\u64ad\u9519\u8bef!EventType=&quot; + type);\n        }\n\n        public static void Broadcast&lt;T, X, Y, Z&gt;(EventType type, T arg1, X arg2, Y arg3, Z arg4)\n        {\n            Delegate del = null;\n            if (m_EventMap.TryGetValue(type, out del))\n            {\n                Callback&lt;T, X, Y, Z&gt; callback = (del as Callback&lt;T, X, Y, Z&gt;);\n                if (callback != null)\n                    callback(arg1, arg2, arg3, arg4);\n            }\n            else\n                throw new Exception(&quot;\u4e8b\u4ef6\u5e7f\u64ad\u9519\u8bef!EventType=&quot; + type);\n        }\n\n        public static void Broadcast&lt;T, X, Y, Z, W&gt;(EventType type, T arg1, X arg2, Y arg3, Z arg4, W arg5)\n        {\n            Delegate del = null;\n            if (m_EventMap.TryGetValue(type, out del))\n            {\n                Callback&lt;T, X, Y, Z, W&gt; callback = (del as Callback&lt;T, X, Y, Z, W&gt;);\n                if (callback != null)\n                    callback(arg1, arg2, arg3, arg4, arg5);\n            }\n            else\n                throw new Exception(&quot;\u4e8b\u4ef6\u5e7f\u64ad\u9519\u8bef!EventType=&quot; + type);\n        }\n\n        #endregion\n\n        #region \u79c1\u6709\u65b9\u6cd5\n\n        private static void DoAddListenerCheck(EventType eventType, Delegate callback)\n        {\n            if (!m_EventMap.ContainsKey(eventType))\n                m_EventMap.Add(eventType, null);\n            Delegate del = m_EventMap[eventType];\n            if (del != null &amp;&amp; del.GetType() != callback.GetType())\n                throw new Exception(&quot;\u6dfb\u52a0\u76d1\u542c\u9519\u8bef: \u59d4\u6258\u7c7b\u578b\u4e0d\u4e00\u81f4\uff01&quot;);\n        }\n\n        private static void DoRemoveListenerCheck(EventType eventType, Delegate callback)\n        {\n            if (callback == null) throw new Exception(&quot;\u79fb\u9664\u76d1\u542c\u9519\u8bef: Callback\u4e3anull!&quot;);\n            Delegate del = null;\n            if (m_EventMap.TryGetValue(eventType, out del))\n            {\n                if (del.GetType() != callback.GetType())\n                    throw new Exception(&quot;\u79fb\u9664\u76d1\u542c\u9519\u8bef: \u59d4\u6258\u7c7b\u578b\u4e0d\u6b63\u786e! eventType=&quot; + eventType);\n            }\n            else\n            {\n                throw new Exception(&quot;\u79fb\u9664\u76d1\u542c\u9519\u8bef: \u6ca1\u6709\u5bf9\u5e94\u7684\u4e8b\u4ef6\u7c7b\u578b\uff01eventType=&quot; + eventType);\n            }\n        }\n\n        #endregion\n    }\n}\n<\/code><\/pre>\n<h2>\u6d4b\u8bd5<\/h2>\n<pre><code class=\"language-csharp line-numbers\">using System;\n\nnamespace EventSystem\n{\n    internal class Program\n    {\n        public static void Main(string[] args)\n        {\n            EventCenter.AddListener(EventType.Update,Update);\n            EventCenter.Broadcast(EventType.Update);\n\n            Console.ReadKey();\n        }\n\n        public static void Update()\n        {\n            Console.WriteLine(&quot;Update&quot;);\n        }\n    }\n}\n<\/code><\/pre>\n<p><img decoding=\"async\" layer-src=\"https:\/\/i.loli.net\/2019\/10\/14\/ufydaR849zANBPY.png\" src=\"https:\/\/i.loli.net\/2019\/10\/14\/ufydaR849zANBPY.png\" alt=\"\u300aC#\uff1a\u57fa\u4e8e\u679a\u4e3e\u7684\u7b80\u6613\u4e8b\u4ef6\u76d1\u542c\u4e0e\u5e7f\u64ad\u7cfb\u7edf\u300b\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee3\u7801 \u59d4\u6258\u5b9a\u4e49 namespace EventSystem { public delegate void Callback(); public delegate void Callback<T>(T arg); public delegate void Callback<T, X>(T arg1, X arg2);\u2026\u2026<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/posts\/91"}],"collection":[{"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/comments?post=91"}],"version-history":[{"count":0,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}