FieldInfo fieldInfo = typeof(Control).GetField("EventCheckedChanged", BindingFlags.NonPublic | BindingFlags.Static);
我在界面上拉了一个checkbox,并且添加了
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
MessageBox.Show("asdf");
}
为什么通关GetField()方法得到的fieldInfo是空值呢?
GetField是字段.就是成员变量,方法不算的要找方法得用那个get方法的.
调试在control.GetType()这一行.然后按shift+f10看gettype返回的对象.可以在里面敲代码看怎么获取到你想要的.
PropertyInfo propertyInfo = control.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
EventHandlerList eventHandlerList = propertyInfo.GetValue(control, new object[] { }) as EventHandlerList;
FieldInfo fieldInfo = typeof(Control).GetField("CheckedChanged", BindingFlags.NonPublic | BindingFlags.Static);
var eventKey = fieldInfo.GetValue(control);
var eventHandler = eventHandlerList[eventKey] as Delegate;
Delegate[] invocationList = eventHandler.GetInvocationList();
您好,上边是我的完全代码,我其实是想拿到invocationList,但是因为fieldInfo获得的是空值,报错了。
您帮忙看下吧,谢谢
自己解决了,有点坑啊,对于checkbox,是另类的写法:
1 FieldInfo fieldInfo = typeof(CheckBox).GetField("EVENT_CHECKEDCHANGED", BindingFlags.NonPublic | BindingFlags.Static);