WinForms/.NET: How to get the event handler attached to an event?

和自甴很熟 提交于 2019-12-25 02:09:53

问题


How can i get the event handler attached to an event?

pseudocode, for example's sake:

public static EventHandler GetChangeUICuesEventHandler(Control SomeControl)
{
   EventHandler changeUICuesEventHandler = SomeControl.ChangeUICues;
   return changeUICuesEventHandler
}

Sample usage (pseudo-code):

Control control = GetControlByName(Console.ReadLine());
button1.ChangeUICues += GetChangeUICuesEventHandler(control);

回答1:


I offer up the following as an alternative path rather than a direct answer... So here goes...

Can you approach this a little differently perhaps by ensuring that your control with the event handler already attached, in this case the parameter someControl, instead implements an interface that defines the attached handler. For example:

public interface ChangUICuesHandler
{
    void OnChangeUICues(object sender, EventArgs e);
}

Then you could use it like so:

Control control = GetControlByName(Console.ReadLine());
if (!(control is ChangeUICuesHandler))
{
    throw new Exception("Input Control does not implement the interface");
}
var handlerControl = control as ChangeUICuesHandler;
button.ChangeUICues += handler.OnChangeUICues;


来源:https://stackoverflow.com/questions/8174203/winforms-net-how-to-get-the-event-handler-attached-to-an-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!