RoutedEventArgs.InvokeEventHandler(Delegate, Object) Method

Definition

When overridden in a derived class, provides a way to invoke event handlers in a type-specific way, which can increase efficiency over the base implementation.

C#
protected virtual void InvokeEventHandler(Delegate genericHandler, object genericTarget);

Parameters

genericHandler
Delegate

The generic handler / delegate implementation to be invoked.

genericTarget
Object

The target on which the provided handler should be invoked.

Examples

The following is pseudocode that illustrates a basic pattern that can be used for implementation. Here, MyRoutedEventHandler is a subclass of RoutedEventHandler.

C#
public class MyRoutedEventArgs : RoutedEventArgs 
{
// other members omitted
    protected override void InvokeEventHandler(Delegate genericHandler, object genericTarget) {
        MyRoutedEventHandler handler = (MyRoutedEventHandler) genericHandler;
        handler(genericTarget, this);
    }
}

Remarks

The base implementation incorporates reflection to determine the right handler for any case where the handler is not literally RoutedEventHandler, and this reflection step does have some performance consequences. Invocations can be made more efficient by not relying on reflection. This is the scenario that motivates this method being available for any routed event arguments class that choose to override it. Implementations should not call the base for this method, because your implementation should already be responsible for invoking the type safe handlers.

Notes to Inheritors

This method is intended to be overridden by derived event data classes to provide more efficient invocation of their delegates. The implementation should cast the provided genericHandler to the type-specific delegate, and then invoke that handler.

The default implementation will attempt to invoke the provided handler, attempting to cast it as RoutedEventHandler. If either genericHandler or genericTarget is provided as null, exceptions will be raised.

Applies to

Produkt Verzie
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10