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});
            }

  • 相关阅读:
    KVM安装以及远程连接
    开博客祭
    CQOI 2021
    琐记——学长们
    大事祭
    关于洛谷与博客园的博客的一些声明
    CSP-S 2020 & NOIP 2020 日记与游记
    调和级数
    快速乘
    二叉堆
  • 原文地址:https://www.cnblogs.com/wdfrog/p/1986686.html
Copyright © 2011-2022 走看看