我正在尝试通过反射获取按钮的点击事件,以下是我的代码:
Assembly assem = Assembly.GetExecutingAssembly();
Type tExForm = btnSave.GetType();
Object exFormAsObj = Activator.CreateInstance(tExForm);
EventInfo evClick = tExForm.GetEvent("Click");
Type tDelegate = evClick.EventHandlerType;
MethodInfo miHandler=
btnSave.GetType().GetMethod("Click",
BindingFlags.NonPublic | BindingFlags.Instance);
Delegate d = Delegate.CreateDelegate(tDelegate, btnSave, miHandler);
MethodInfo addHandler = evClick.GetAddMethod();
Object[] addHandlerArgs = { d };
addHandler.Invoke(exFormAsObj, addHandlerArgs);
然而,miHandler
变量总是为空!
注意:我关心的主要问题是如何在运行时调用特定控件的事件,有没有办法?例如,当用户按F1键时,我想选择触发哪个单击事件。