zoukankan      html  css  js  c++  java
  • 使用反射动态绑定与移除方法

            public void AddPublisher(object publisher, string eventName)
            {
                publishers.Add(publisher);
                EventInfo targetEvent = publisher.GetType().GetEvent(eventName);
                GuardEventExists(eventName, publisher, targetEvent);

                MethodInfo addEventMethod = targetEvent.GetAddMethod();
                GuardAddMethodExists(targetEvent);//检测是否存在这样的方法

                EventHandler newSubscriber = OnPublisherFiring;
                addEventMethod.Invoke(publisher, new object[] { newSubscriber });
            }

            public void RemovePublisher(object publisher, string eventName)
            {
                publishers.Remove(publisher);
                EventInfo targetEvent = publisher.GetType().GetEvent(eventName);
                GuardEventExists(eventName, publisher, targetEvent);

                MethodInfo removeEventMethod = targetEvent.GetRemoveMethod();
                GuardRemoveMethodExists(targetEvent);

                EventHandler subscriber = OnPublisherFiring;
                removeEventMethod.Invoke(publisher, new object[] {subscriber});
            }

  • 相关阅读:
    Unity Shader 之 uv动画
    c++源文件后缀名问题
    Unity Shader 之 透明效果
    正则表达式
    Unity Shader基础
    Unity Shader 之 渲染流水线
    2017/11/22 Leetcode 日记
    2017/11/21 Leetcode 日记
    2017/11/13 Leetcode 日记
    2017/11/20 Leetcode 日记
  • 原文地址:https://www.cnblogs.com/wdfrog/p/1986686.html
Copyright © 2011-2022 走看看