/// <summary> /// Builds a delegate from a synchronous action. /// </summary> /// <typeparam name="TAttributed">Type that contains [EventHandler] methods. This should match DeclaringType property.</typeparam> /// <typeparam name="TEvent">Type of event that is handled by the EventHandlerAttributeMethod. This should match EventType property.</typeparam> /// <param name="attributedObjectFactory">Factory delegate which provides an instance of a class that contains methods marked with [EventHandler] attribute.</param> /// <returns>Delegate that handles an event.</returns> private MessageHandlerDelegate BuildWrappedSyncDelegate <TAttributed, TEvent>(Func <object> attributedObjectFactory) where TAttributed : class where TEvent : class { // Create an expression that will invoke the event handler method of a given instance. ParameterExpression instanceParameterExpression = Expression.Parameter(typeof(TAttributed), "instance"); ParameterExpression eventParameterExpression = Expression.Parameter(typeof(TEvent), "event"); MethodCallExpression callExpression = Expression.Call(instanceParameterExpression, MethodInfo, eventParameterExpression); // Lambda signature: // (instance, @event) => instance.HandleEvent(@event); Action <TAttributed, TEvent> action = Expression.Lambda <Action <TAttributed, TEvent> >(callExpression, new[] { instanceParameterExpression, eventParameterExpression }).Compile(); return(EventHandlerDelegateBuilder.FromDelegate(attributedObjectFactory, action, yieldSynchronousExecution: YieldSynchronousExecution)); }
/// <summary> /// Builds a delegate from an asynchronous (non-cancellable) action. /// </summary> /// <typeparam name="TAttributed">Type that contains [EventHandler] methods. This should match DeclaringType property.</typeparam> /// <typeparam name="TEvent">Type of event that is handled by the EventHandlerAttributeMethod. This should match EventType property.</typeparam> /// <param name="attributedObjectFactory">Factory delegate which provides an instance of a class that contains methods marked with [EventHandler] attribute.</param> /// <returns>Delegate that handles an event.</returns> private MessageHandlerDelegate BuildNonCancellableAsyncDelegate <TAttributed, TEvent>(Func <object> attributedObjectFactory) where TAttributed : class where TEvent : class { // Create an expression that will invoke the event handler method of a given instance. ParameterExpression instanceParameterExpression = Expression.Parameter(typeof(TAttributed), "instance"); ParameterExpression eventParameterExpression = Expression.Parameter(typeof(TEvent), "event"); MethodCallExpression callExpression = Expression.Call(instanceParameterExpression, MethodInfo, eventParameterExpression); // Lambda signature: // (instance, @event) => instance.HandleEventAsync(@event); Func <TAttributed, TEvent, Task> nonCancellableAsyncDelegate = Expression.Lambda <Func <TAttributed, TEvent, Task> >(callExpression, new[] { instanceParameterExpression, eventParameterExpression }).Compile(); return(EventHandlerDelegateBuilder.FromDelegate(attributedObjectFactory, nonCancellableAsyncDelegate)); }