c# – 检查事件是否有任何监听器?
发布时间:2020-12-15 18:05:46 所属栏目:百科 来源:网络整理
导读:是否可以检测事件是否有任何监听器? (我需要处理我的事件提供者对象,如果没有人需要的话) 解决方法 假设课程在第三方库中,无法修改: public class Data { public event EventHandler OnSave; //other members } 在你的程序中 Data d = new Data(); d.OnSav
是否可以检测事件是否有任何监听器? (我需要处理我的事件提供者对象,如果没有人需要的话)
解决方法
假设课程在第三方库中,无法修改:
public class Data { public event EventHandler OnSave; //other members } 在你的程序中 Data d = new Data(); d.OnSave += delegate { Console.WriteLine("event"); }; var handler = typeof(Data).GetField("OnSave",BindingFlags.NonPublic | BindingFlags.Instance).GetValue(d) as Delegate; if (handler == null) { //no subscribers } else { var subscribers = handler.GetInvocationList(); //now you have the subscribers } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |