如题:
  如:
事件A ;委托B;方法C,D
 怎样判断A 是否包含C?  委托,事件我不太清楚,可能问题提的就有问题
请大侠指点

解决方案 »

  1.   

    那这样问
      A是否被B(C)所绑定?
      

  2.   


            static void Main(string[] args)
            {
                Button btn = new Button();
                btn.Click += new EventHandler(btn_Click2);
                PropertyInfo pi = (typeof(Button)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
                EventHandlerList ehl = (EventHandlerList)pi.GetValue(btn, null);
                FieldInfo fieldInfo = (typeof(Control)).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);
                Delegate d = ehl[fieldInfo.GetValue(null)];            foreach (Delegate del in d.GetInvocationList())
                {
                    Console.WriteLine(del.Method.Name == "btn_Click");
                }
            }        static void btn_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Hello");
            }        static void btn_Click2(object sender, EventArgs e)
            {
                MessageBox.Show("Hello Again");
            }
      

  3.   


    查了查PRopertyInfo,EventHandlerList ,FieldInfo ,还是没看明白
      

  4.   

    你的意思是,判断委托B和C、D是否符合事件A的签名,是要实现这样的效果吗?
      

  5.   


    ojlovecd 代码解释如下:            //为Button1绑定一个事件处理程序
                Button btn = new Button();
                btn.Click += new EventHandler(button2_Click);
                //获取Button类定义的所有事件的信息
                PropertyInfo pi = (typeof(Button)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
                //获取Button对象btn的事件处理程序列表
                EventHandlerList ehl = (EventHandlerList)pi.GetValue(btn, null);
                //获取Control类Click事件的字段信息
                FieldInfo fieldInfo = (typeof(Control)).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);
                //用获取的Click事件的字段信息,去匹配btn对象的事件处理程序列表,获取btn对象Click事件的委托对象
                //事件使用委托定义的,C#中的委托时多播委托,可以绑定多个事件处理程序,当事件发生时,这些事件处理程序被依次执行
                //因此Delegate对象,有一个GetInvocationList方法,用来获取这个委托已经绑定的所有事件处理程序
                Delegate d = ehl[fieldInfo.GetValue(null)];            foreach (Delegate del in d.GetInvocationList())
                {
                    //判断一下某个事件处理程序是否已经被绑定到Click事件上
                    Console.WriteLine(del.Method.Name == "button1_Click");
                }
      

  6.   

    fdsfgsadfasdfasdfasfasdfas[align=center]***********************************************************                    欢迎使用 CSDN 小秘书
                  http://blog.csdn.net/whowhen21***********************************************************[/align]