/// <summary> /// Subscribes the given action to the given event/selection type. Since the eventType is determined at runtime, the event/selection arguments are not available : /// you can just subscribe a parameterless action. /// </summary> /// <param name="eventAggregator">The event aggregator instance from which to resolve the event.</param> /// <param name="eventType">The type of the event/selection to which the subscription is made. Supported types are types that extend CompositePresentationEvent or SelectionBase.</param> /// <param name="action">The action that is to be invoked when the specified event is fired / the specified selection changes.</param> /// <param name="threadOption">The thread on which the event should be handled.</param> /// <param name="keepSubscriberReferenceAlive">A flag indicating if a reference to the subscription should be held or not.</param> public static SubscriptionToken Subscribe(this IEventAggregator eventAggregator, Type eventType, Action action, ThreadOption threadOption = ThreadOption.PublisherThread, bool keepSubscriberReferenceAlive = true) { eventAggregator.AssertNotNull(nameof(eventAggregator)); eventType.AssertParameterNotNull(nameof(eventType)); action.AssertParameterNotNull(nameof(action)); if (eventType.IsSubclassOfRawGeneric(typeof(CompositePresentationEvent <>))) { var payloadType = eventType.GetBaseTypeGenericArgument(typeof(CompositePresentationEvent <>)); var subscriptionMethod = typeof(EventAggregatorHelpers).GetMethod(nameof(EventAggregatorHelpers.SubscribeToEvent)).MakeGenericMethod(new Type[] { eventType, payloadType }); return((SubscriptionToken)subscriptionMethod.Invoke(null, new object[] { eventAggregator, threadOption, keepSubscriberReferenceAlive, action })); } else if (eventType.IsSubclassOfRawGeneric(typeof(SelectionBase <>))) { var payloadType = eventType.GetBaseTypeGenericArgument(typeof(SelectionBase <>)); var subscriptionMethod = typeof(EventAggregatorHelpers).GetMethod(nameof(EventAggregatorHelpers.SubscribeToSelection)).MakeGenericMethod(new Type[] { eventType, payloadType }); return((SubscriptionToken)subscriptionMethod.Invoke(null, new object[] { eventAggregator, threadOption, keepSubscriberReferenceAlive, action })); } else { throw new NotSupportedException($"{eventType.Name} is not a supported event type. Supported event types are types which extend CompositePresentationEvent or SelectionBase."); } }
/// <summary> /// Returns the instance of the specified event type (from the container), performing a registration check first. /// It is the equivalent of EventAggregator.GetEvent'1, but this can be called on runtime. /// </summary> /// <param name="eventAggregator">The event aggregator instance from which to resolve the event.</param> /// <param name="eventType">The type of the event that is to be resolved. Supported types are types that extend CompositePresentationEvent or SelectionBase.</param> /// <returns></returns> public static EventBase GetEvent(this IEventAggregator eventAggregator, Type eventType) { eventAggregator.AssertNotNull(nameof(eventAggregator)); eventType.AssertParameterNotNull(nameof(eventType)); if (!(eventType.IsSubclassOfRawGeneric(typeof(CompositePresentationEvent <>)) || eventType.IsSubclassOfRawGeneric(typeof(SelectionBase <>)))) { throw new NotSupportedException($"Error : {eventType.Name} is not a supported eventType. Supported types are either subtypes of CompositePresentationEvent<T> (events) or " + $"subtypes of SelectionBase<T>(selections)."); } var eventGetter = typeof(IEventAggregator).GetMethod(nameof(eventAggregator.GetEvent)).MakeGenericMethod(eventType); return((EventBase)eventGetter.Invoke(eventAggregator, new object[] { })); }