C#如何使用反射获取事件的响应方法? c#反射 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 MouseDown += Down; MessageBox.Show(getDelegateByControl(this, "EventMouseDown").Method.Name); //显示Down其中的getDelegateByControl: Delegate getDelegateByControl(Control Control, string EventName) { var events = typeof(Control).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(Control) as EventHandlerList; var key = typeof(Control).GetField(EventName, BindingFlags.Static | BindingFlags.NonPublic); if (key == null) { throw new ArgumentException("提供的事件名不存在。", "EventName"); } return events[key.GetValue(null)]; } public Form3() { InitializeComponent(); button1.MouseClick += new MouseEventHandler(button1_MouseClick); foreach (Control c in this.Controls) { if (c is Button) { Button btn = c as Button; if(btn.Name == "button1") { Delegate[] _ControlDelegate = GetObjectEventList(button1, "EventMouseClick"); Delegate d = _ControlDelegate[0]; MethodInfo info = d.Method; string str = info.Name; } } } } private Delegate[] GetObjectEventList(Control p_Control, string p_EventName) { PropertyInfo _PropertyInfo = p_Control.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic); if (_PropertyInfo != null) { object _EventList = _PropertyInfo.GetValue(p_Control, null); if (_EventList != null && _EventList is EventHandlerList) { EventHandlerList _List = (EventHandlerList)_EventList; FieldInfo _FieldInfo = (typeof(Control)).GetField(p_EventName, BindingFlags.Static | BindingFlags.NonPublic); if (_FieldInfo == null) return null; Delegate _ObjectDelegate = _List[_FieldInfo.GetValue(p_Control)]; if (_ObjectDelegate == null) return null; return _ObjectDelegate.GetInvocationList(); } } return null; } void button1_MouseClick(object sender, MouseEventArgs e) { throw new NotImplementedException(); } 上面代码中string str = info.Name; str的值为button1_MouseClick 你的代码中的typeof(Control).GetField(EventName, BindingFlags.Static | BindingFlags.NonPublic)这一句我改成了typeof(Control).GetFields(BindingFlags.Static | BindingFlags.NonPublic)竟然发现,这个集合里面没有一个元素 你这个代码的第一段我改了一下是如下Delegate[] _ControlDelegate = GetObjectEventList(this.Controls[1], "EventMouseDown"); Delegate d = _ControlDelegate[0]; MethodInfo info = d.Method; string str = info.Name;然后运行的时候_ControlDelegate这个值为空 其他的都很正常,但是Delegate _ObjectDelegate = _List[_FieldInfo.GetValue(p_Control)];运行到里的时候,右边的值为空 你这个代码的第一段我改了一下是如下Delegate[] _ControlDelegate = GetObjectEventList(this.Controls[1], "EventMouseDown"); Delegate d = _ControlDelegate[0]; MethodInfo info = d.Method; string str = info.Name;然后运行的时候_ControlDelegate这个值为空你确定你的这个this.Controls[1]注册了MouseDown事件? 好吧,我脑残了,我是再设计页面点击的事件,想着这个this.Controls[1]是覆盖窗体的,还以为就是这个注册了MouseDown事件,没想到刚刚一看才发现,原来是form注册的事件不是this.Controls[1]这个 Atlas的命名空间 VS2005 错误信息 数据库问题,急!!! 求详细的C# 根据链接地址 实现文件下载实例 通过saveFileDialog1我怎么取得路径,因为我想用它保存一个文件??,谢谢 一个日期设置的疑问 from自动关闭问题? 求C#关于Oracle 9i的select、delete、update、insert的操作代码?谢谢了 不用画布,怎么已知图片上画图? 请教插入Access表的一个记录的操作。 服务端与客户端消息传递 数据库连接问题,求教!
MessageBox.Show(getDelegateByControl(this, "EventMouseDown").Method.Name); //显示Down
其中的getDelegateByControl:
Delegate getDelegateByControl(Control Control, string EventName)
{
var events =
typeof(Control).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(Control)
as EventHandlerList; var key = typeof(Control).GetField(EventName, BindingFlags.Static | BindingFlags.NonPublic);
if (key == null)
{
throw new ArgumentException("提供的事件名不存在。", "EventName");
} return events[key.GetValue(null)];
}
{
InitializeComponent(); button1.MouseClick += new MouseEventHandler(button1_MouseClick);
foreach (Control c in this.Controls)
{
if (c is Button)
{
Button btn = c as Button;
if(btn.Name == "button1")
{
Delegate[] _ControlDelegate = GetObjectEventList(button1, "EventMouseClick");
Delegate d = _ControlDelegate[0];
MethodInfo info = d.Method;
string str = info.Name;
}
}
}
} private Delegate[] GetObjectEventList(Control p_Control, string p_EventName)
{
PropertyInfo _PropertyInfo = p_Control.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
if (_PropertyInfo != null)
{
object _EventList = _PropertyInfo.GetValue(p_Control, null);
if (_EventList != null && _EventList is EventHandlerList)
{
EventHandlerList _List = (EventHandlerList)_EventList;
FieldInfo _FieldInfo = (typeof(Control)).GetField(p_EventName, BindingFlags.Static | BindingFlags.NonPublic);
if (_FieldInfo == null) return null;
Delegate _ObjectDelegate = _List[_FieldInfo.GetValue(p_Control)];
if (_ObjectDelegate == null) return null;
return _ObjectDelegate.GetInvocationList();
}
}
return null;
} void button1_MouseClick(object sender, MouseEventArgs e)
{
throw new NotImplementedException();
}
我改成了typeof(Control).GetFields(BindingFlags.Static | BindingFlags.NonPublic)竟然发现,这个集合里面没有一个元素
Delegate d = _ControlDelegate[0];
MethodInfo info = d.Method;
string str = info.Name;
然后运行的时候_ControlDelegate这个值为空
Delegate d = _ControlDelegate[0];
MethodInfo info = d.Method;
string str = info.Name;
然后运行的时候_ControlDelegate这个值为空你确定你的这个this.Controls[1]注册了MouseDown事件?