protected virtual void InvokeMethods(IEnumerable <KeyValuePair <Type, AutoSubscriberConsumerInfo[]> > subscriptionInfos, string dispatchName, MethodInfo genericBusSubscribeMethod, Func <Type, Type> subscriberTypeFromMessageTypeDelegate) { foreach (var kv in subscriptionInfos) { foreach (var subscriptionInfo in kv.Value) { var dispatchMethod = AutoSubscriberMessageDispatcher.GetType() .GetMethod(dispatchName, BindingFlags.Instance | BindingFlags.Public) .MakeGenericMethod(subscriptionInfo.MessageType, subscriptionInfo.ConcreteType); #if !NETFX var dispatchDelegate = dispatchMethod.CreateDelegate( subscriberTypeFromMessageTypeDelegate(subscriptionInfo.MessageType), AutoSubscriberMessageDispatcher); #else var dispatchDelegate = Delegate.CreateDelegate(subscriberTypeFromMessageTypeDelegate(subscriptionInfo.MessageType), AutoSubscriberMessageDispatcher, dispatchMethod); #endif var subscriptionAttribute = GetSubscriptionAttribute(subscriptionInfo); var subscriptionId = subscriptionAttribute != null ? subscriptionAttribute.SubscriptionId : GenerateSubscriptionId(subscriptionInfo); var busSubscribeMethod = genericBusSubscribeMethod.MakeGenericMethod(subscriptionInfo.MessageType); Action <ISubscriptionConfiguration> configAction = GenerateConfigurationAction(subscriptionInfo); busSubscribeMethod.Invoke(bus, new object[] { subscriptionId, dispatchDelegate, configAction }); } } }
/// <summary> /// Registers all consumers in passed assembly. The actual Subscriber instances is /// created using <seealso cref="CreateConsumer"/>. The SubscriptionId per consumer /// method is determined by <seealso cref="GenerateSubscriptionId"/> or if the method /// is marked with <see cref="AutoSubscriberConsumerAttribute"/> with a custom SubscriptionId. /// </summary> /// <param name="assemblies">The assembleis to scan for consumers.</param> public virtual void Subscribe(params Assembly[] assemblies) { Preconditions.CheckAny(assemblies, "assemblies", "No assemblies specified."); var genericBusSubscribeMethod = GetSubscribeMethodOfBus(); var subscriptionInfos = GetSubscriptionInfos(assemblies.SelectMany(a => a.GetTypes())); foreach (var kv in subscriptionInfos) { foreach (var subscriptionInfo in kv.Value) { var dispatchMethod = AutoSubscriberMessageDispatcher.GetType() .GetMethod(DispatchMethodName, BindingFlags.Instance | BindingFlags.Public) .MakeGenericMethod(subscriptionInfo.MessageType, subscriptionInfo.ConcreteType); var dispatchMethodType = typeof(Action <>).MakeGenericType(subscriptionInfo.MessageType); var dispatchDelegate = Delegate.CreateDelegate(dispatchMethodType, AutoSubscriberMessageDispatcher, dispatchMethod); var subscriptionAttribute = GetSubscriptionAttribute(subscriptionInfo); var subscriptionId = subscriptionAttribute != null ? subscriptionAttribute.SubscriptionId : GenerateSubscriptionId(subscriptionInfo); var busSubscribeMethod = genericBusSubscribeMethod.MakeGenericMethod(subscriptionInfo.MessageType); busSubscribeMethod.Invoke(bus, new object[] { subscriptionId, dispatchDelegate }); } } }
protected void InvokeMethods(IEnumerable <KeyValuePair <Type, AutoSubscriberConsumerInfo[]> > subscriptionInfos, string dispatchName, Func <Type, Type> subscriberTypeFromMessageTypeDelegate, Action <AutoSubscriberConsumerInfo, Delegate> subscribeMethod) { foreach (var kv in subscriptionInfos) { foreach (var subscriptionInfo in kv.Value) { var dispatchMethod = AutoSubscriberMessageDispatcher.GetType() .GetMethod(dispatchName, BindingFlags.Instance | BindingFlags.Public) .MakeGenericMethod(subscriptionInfo.MessageType, subscriptionInfo.ConcreteType); var dispatchDelegate = Delegate.CreateDelegate(subscriberTypeFromMessageTypeDelegate(subscriptionInfo.MessageType), AutoSubscriberMessageDispatcher, dispatchMethod); subscribeMethod(subscriptionInfo, dispatchDelegate); } } }