non so perché la gente dice che non è possibile:
Diciamo che si desidera disattivare qualsiasi evento temporaneo , è possibile creare un metodo come questo:
static Delegate[] DisableEvents(this Control ctrl, string eventName)
{
PropertyInfo propertyInfo = ctrl.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
EventHandlerList eventHandlerList = propertyInfo.GetValue(ctrl, new object[] { }) as EventHandlerList;
FieldInfo fieldInfo = typeof(Control).GetField("Event"+eventName, BindingFlags.NonPublic | BindingFlags.Static);
object eventKey = fieldInfo.GetValue(ctrl);
var eventHandler = eventHandlerList[eventKey] as Delegate;
Delegate[] invocationList = eventHandler.GetInvocationList();
foreach (EventHandler item in invocationList)
{
ctrl.GetType().GetEvent(eventName).RemoveEventHandler(ctrl, item);
}
return invocationList;
}|
si può chiamare in questo modo:
var events = textbox1.DisableEvents("GotFocus")
Se si desidera aggiungerli di nuovo, è sufficiente scorrere l'elenco degli eventi.
fonte
2014-10-28 20:56:05
possibile duplicato di [Come posso ottenere un elenco dei delegati sottostanti da un evento utilizzando la riflessione?] (Http://stackoverflow.com/questions/6501288/how-can-i -get-a-list-of-the-underlying-delegates-from-an-event-using-reflection) – Heinzi