110,538
社区成员
发帖
与我相关
我的任务
分享
Label li =new Label();
MouseDown += new MouseEventHandler(this.Down);
foreach (Control c in shapeContainer1.Controls)
{
.........
}
Delegate[] _ControlDelegate = GetObjectEventList(this.Controls[1], "EventMouseDown");
Delegate d = _ControlDelegate[0];
MethodInfo info = d.Method;
string str = info.Name;
然后运行的时候_ControlDelegate这个值为空[/quote]你确定你的这个this.Controls[1]注册了MouseDown事件?[/quote]
好吧,我脑残了,我是再设计页面点击的事件,想着这个this.Controls[1]是覆盖窗体的,还以为就是这个注册了MouseDown事件,没想到刚刚一看才发现,原来是form注册的事件不是this.Controls[1]这个。。。。。。Delegate[] _ControlDelegate = GetObjectEventList(this.Controls[1], "EventMouseDown");
Delegate d = _ControlDelegate[0];
MethodInfo info = d.Method;
string str = info.Name;
然后运行的时候_ControlDelegate这个值为空[/quote]你确定你的这个this.Controls[1]注册了MouseDown事件?Delegate[] _ControlDelegate = GetObjectEventList(this.Controls[1], "EventMouseDown");
Delegate d = _ControlDelegate[0];
MethodInfo info = d.Method;
string str = info.Name;
然后运行的时候_ControlDelegate这个值为空[/quote]
其他的都很正常,但是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这个值为空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();
}
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)];
}