这个问题来自论坛,原文为如何判断事件已经被注册过?
用反射取出事件绑定的委托实例,然后用GetInvocationList就可以得到所有注册的方法了。
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace WindowsApplication19
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load+=new EventHandler(Form1_Load1);
this.Load+=new EventHandler(Form1_Load2);
PropertyInfo propertyInfo = (typeof(Form)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(this, null);
FieldInfo fieldInfo = (typeof(For