这个问题来自论坛,原文为如何判断事件已经被注册过?
用反射取出事件绑定的委托实例,然后用GetInvocationList就可以得到所有注册的方法了。
代码
view plaincopy to clipboardprint?
01.using System;02.using System.Collections.Generic;03.using System.ComponentModel;04.using System.Data;05.using System.Drawing;06.using System.Text;07.using System.Windows.Forms;08.using System.Reflection;09.namespace WindowsApplication1910.{11. public partial class Form1 : Form12. {13. public Form1()14. {15. InitializeComponent();16.17. this.Load+=new EventHandler(Form1_Load1);18. this.Load+=new EventHandler(Form1_Load2);19.20. PropertyInfo propertyInfo = (typeof(Form)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);21. EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(this, null);22. FieldInfo fieldInfo = (typeof(Form)).GetField("EVENT_LOAD", BindingFlags.Static | BindingFlags.NonPublic);23.24. Delegate d = eventHandlerList[fieldInfo.GetValue(null)];25.26. if (d != null)27. {28. foreach (Delegate de in d.GetInvocationList())29. Console.WriteLine(de.Method.Name);30. }31. }32. private void Form1_Load1(object sender, EventArgs e)33. {34. //什么也不干35. }36. private void Form1_Load2(object sender, EventArgs e)37. {38. //什么也不干39. }40. }41.}
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索form
, 事件
, using
, load
, 注册
system
c站、c语言、cf、ch、c罗,以便于您获取更多的相关知识。
时间: 2024-10-29 04:21:00