zjbonrace 2015-04-24 11:41 采纳率: 100%
浏览 1631
已采纳

link中如何判断一个事件里面有几个方法?如何选择性调用?

link中如何判断一个事件里面有几个方法?如何选择性调用?

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-04-30 09:50
    关注
     using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                button1.Click += button1_Click_1;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("button1_0");
            }
            private void button1_Click_1(object sender, EventArgs e)
            {
                MessageBox.Show("button1_1");
            }
            public static Delegate[] GetObjectEventList(object p_Object, string p_EventName, Type p_EventType)
            {
                PropertyInfo _PropertyInfo = p_Object.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
                if (_PropertyInfo != null)
                {
                    object _EventList = _PropertyInfo.GetValue(p_Object, null);
                    if (_EventList != null && _EventList is EventHandlerList)
                    {
                        EventHandlerList _List = (EventHandlerList)_EventList;
                        FieldInfo _FieldInfo = p_EventType.GetField(p_EventName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
                        if (_FieldInfo == null) return null;
                        Delegate _ObjectDelegate = _List[_FieldInfo.GetValue(p_Object)];
                        if (_ObjectDelegate == null) return null;
                        return _ObjectDelegate.GetInvocationList();
                    }
                }
                return null;
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Delegate[] _List = GetObjectEventList(button1, "EventClick", typeof(Control));
                _List[1].Method.Invoke(this, new object[] { sender, e });
            }
        }
    }
    
    

    这段代码中点button2,只调用button1_Click_1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b