日期:2014-05-18 浏览次数:21080 次
using System; using System.Collections.Generic; using System.Reflection; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("button1"); } private void button2_Click(object sender, EventArgs e) { Delegate[] _List = GetObjectEventList(button1, "EventClick", typeof(Control)); foreach (var item in _List) { item.DynamicInvoke(sender, e); } } 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; } } }
------解决方案--------------------
是想得到 Click 这个event吧,event 就是委托,不过包了一层,你得到了也干不了啥,出了加减委托
------解决方案--------------------
using System; using System.Collections.Generic; using System.Reflection; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("button1"); } private void button2_Click(object sender, EventArgs e) { Delegate[] _List = GetObjectEventList(button1, "EventClick", typeof(Control)); foreach (var item in _List) { MessageBox.Show(item.Method.ToS