/// <summary> /// Initializes a new instance of the <see cref="Subscription"/> class /// </summary> internal Subscription( WorkItemSubscriptions workItemSubscriptions, object subscriber, string handlerMethodName, Type[] parameterTypes, ThreadOption threadOption) { this.wrSubscriber = new WeakReference(subscriber); this.handlerMethodName = handlerMethodName; this.threadOption = threadOption; this.workItemSubscriptions = workItemSubscriptions; MethodInfo miHandler = GetMethodInfo(subscriber, handlerMethodName, parameterTypes); if (miHandler == null) { throw new EventBrokerException(String.Format(CultureInfo.CurrentCulture, Properties.Resources.SubscriberHandlerNotFound, handlerMethodName, subscriber.GetType().ToString())); } else if (miHandler.IsStatic) { throw new EventBrokerException(String.Format(CultureInfo.CurrentCulture, Properties.Resources.CannotRegisterStaticSubscriptionMethods, miHandler.DeclaringType.FullName, miHandler.Name)); } this.typeHandle = subscriber.GetType().TypeHandle; this.methodHandle = miHandler.MethodHandle; ParameterInfo[] parameters = miHandler.GetParameters(); if (IsValidEventHandler(parameters)) { ParameterInfo pInfo = miHandler.GetParameters()[1]; Type pType = pInfo.ParameterType; handlerEventArgsType = typeof(EventHandler<>).MakeGenericType(pType); } else { throw new EventBrokerException(String.Format(CultureInfo.CurrentCulture, Properties.Resources.InvalidSubscriptionSignature, miHandler.DeclaringType.FullName, miHandler.Name)); } if (threadOption == ThreadOption.UserInterface) { // If there's a syncronization context (i.e. the WindowsFormsSynchronizationContext // created to marshal back to the thread where a control was initially created // in a particular thread), capture it to marshal back to it through the // context, that basically goes through a Post/Send. if (SynchronizationContext.Current != null) { syncContext = SynchronizationContext.Current; } } }
/// <summary> /// Adds a subcription to this <see cref="EventTopic"/>. /// </summary> /// <param name="subscriber">The object that contains the method that will handle the <see cref="EventTopic"/>.</param> /// <param name="handlerMethodName">The name of the method on the subscriber that will handle the <see cref="EventTopic"/>.</param> /// <param name="workItem">The <see cref="WorkItem"/> where the subscriber is.</param> /// <param name="threadOption">A <see cref="ThreadOption"/> value indicating how the handler method should be called.</param> /// <param name="parameterTypes">Defines the types and order of the parameters for the subscriber. For none pass null. /// Use this overload when there are several methods with the same name on the subscriber.</param> public virtual void AddSubscription(object subscriber, string handlerMethodName, Type[] parameterTypes, WorkItem workItem, ThreadOption threadOption) { Guard.ArgumentNotNull(subscriber, "subscriber"); Guard.ArgumentNotNullOrEmptyString(handlerMethodName, "handlerMethodName"); Guard.EnumValueIsDefined(typeof(ThreadOption), threadOption, "threadOption"); Clean(); WorkItemSubscriptions wis = FindWorkItemSubscription(workItem); if (wis == null) { wis = new WorkItemSubscriptions(workItem); workItemSubscriptions.Add(wis); } wis.AddSubscription(subscriber, handlerMethodName, parameterTypes, threadOption); if (traceSource != null) traceSource.TraceInformation(Properties.Resources.EventTopicTraceSubscriptionAdded, name, handlerMethodName, subscriber.GetType().ToString()); }
/// <summary> /// Initializes a new instance of the <see cref="Subscription"/> class. /// </summary> internal Subscription( WorkItemSubscriptions workItemSubscriptions, object subscriber, string handlerMethodName, ThreadOption threadOption) : this(workItemSubscriptions, subscriber, handlerMethodName, null, threadOption) { }