/// <summary>
 /// 向管理器发送主题。
 /// </summary>
 /// <typeparam name="TSubject"></typeparam>
 /// <param name="subject">主题内容。</param>
 public void Publish <TSubject>(TSubject subject) where TSubject : class
 {
     if (subscribers.TryGetValue(TopicHelper.GetTopicName(typeof(TSubject)), out List <Delegate> list))
     {
         if (list != null)
         {
             list.ForEach(s => s.DynamicInvoke(subject));
         }
     }
 }
Exemple #2
0
        /// <summary>
        /// 添加一个订阅方法。
        /// </summary>
        /// <param name="subjectType">主题的类型。</param>
        /// <param name="subscriber">读取主题的方法。</param>
        public void AddSubscriber(Type subjectType, Delegate subscriber)
        {
            Guard.ArgumentNull(subscriber, nameof(subscriber));

            var list = subscribers.GetOrAdd(TopicHelper.GetTopicName(subjectType), () => new List <Delegate>());

            if (list != null)
            {
                list.Add(subscriber);
            }
        }
Exemple #3
0
        /// <summary>
        /// 添加一个订阅方法。
        /// </summary>
        /// <typeparam name="TSubject"></typeparam>
        /// <param name="subscriber">读取主题的方法。</param>
        public void AddSubscriber <TSubject>(Action <TSubject> subscriber) where TSubject : class
        {
            Guard.ArgumentNull(subscriber, nameof(subscriber));

            var list = subscribers.GetOrAdd(TopicHelper.GetTopicName(typeof(TSubject)), () => new List <Delegate>());

            if (list != null)
            {
                list.Add(subscriber);
            }
        }
Exemple #4
0
        /// <summary>
        /// 添加一个异步的订阅方法。
        /// </summary>
        /// <typeparam name="TSubject"></typeparam>
        /// <param name="subscriber">读取主题的方法。</param>
        public void AddAsyncSubscriber <TSubject>(Func <TSubject, Task> subscriber) where TSubject : class
        {
            Guard.ArgumentNull(subscriber, nameof(subscriber));

            _subscribers.AddAsyncSubscriber(typeof(TSubject), TopicHelper.GetTopicName(typeof(TSubject)), subscriber);
        }
Exemple #5
0
 /// <summary>
 /// 向管理器发送主题。
 /// </summary>
 /// <typeparam name="TSubject"></typeparam>
 /// <param name="subject">主题内容。</param>
 public void Publish <TSubject>(TSubject subject) where TSubject : class
 {
     _subscribers.Accept(TopicHelper.GetTopicName(typeof(TSubject)), subject);
 }
Exemple #6
0
 /// <summary>
 /// 移除相关的订阅方法。
 /// </summary>
 /// <param name="subjectType">主题的类型。</param>
 public void RemoveSubscriber(Type subjectType)
 {
     _subscribers.Remove(TopicHelper.GetTopicName(subjectType));
 }
Exemple #7
0
        /// <summary>
        /// 添加一个订阅方法。
        /// </summary>
        /// <param name="subjectType">主题的类型。</param>
        /// <param name="subscriber">读取主题的方法。</param>
        public void AddSubscriber(Type subjectType, Delegate subscriber)
        {
            Guard.ArgumentNull(subscriber, nameof(subscriber));

            _subscribers.AddSyncSubscriber(subjectType, TopicHelper.GetTopicName(subjectType), subscriber);
        }
Exemple #8
0
 public SubjectData(Type type, object data)
 {
     Name = TopicHelper.GetTopicName(type);
     Data = data;
 }
Exemple #9
0
 /// <summary>
 /// 移除相关的订阅方法。
 /// </summary>
 /// <param name="subjectType">主题的类型。</param>
 public void RemoveSubscriber(Type subjectType)
 {
     subscribers.TryRemove(TopicHelper.GetTopicName(subjectType), out List <Delegate> delegates);
 }