Пример #1
0
        private void ResolveConstructorInjection(IComponentInfo info, Type componentType)
        {
            var key = componentType.FullName + ":ctorInjections";

            info.ExtendedProperties[key] = componentType
                                           .GetConstructors()
                                           .Where(p => !ignoreVisitor.VisitConstructor(p))
                                           .Select(p => AttributeProviderInspector.InspectConstructor(info, Kernel, p))
                                           .OrderBy(p => p.IsMarkedInjection)
                                           .OrderByDescending(p => p.Dependencies.Length)
                                           .ToArray();
        }
Пример #2
0
        /// <summary>
        /// 订阅消息
        /// </summary>
        /// <param name="bus">消息总线</param>
        /// <param name="subscriber">订阅者,方法上标记有SubscribeAttribute 标签的</param>
        /// <returns></returns>
        public static IDisposable Subscribe(this IMessageBus bus, object subscriber)
        {
            Guard.NotNull(bus, "bus");
            Guard.NotNull(subscriber, "subscriber");

            var         key = subscriber.GetType().TypeHandle;
            IDisposable unsubscribers;

            lock (subscriberTypes)
            {
                if (subscriberTypes.TryGetValue(key, out unsubscribers))
                {
                    return(unsubscribers);
                }

                var subscribers = AttributeProviderInspector.InspectSubscribeMethods(subscriber.GetType());

                if (subscribers == null || subscribers.Length == 0)
                {
                    subscriberTypes.Add(key, Disposable.Empty);
                    return(Disposable.Empty);
                }

                var disposeArray = new CompositeDisposable();

                var disCollector = subscriber as IDisposeCollector;
                var compositeDis = subscriber as ICompositeDisposable;
                foreach (var item in subscribers)
                {
                    var unsubscriber = bus.Subscribe(item.Factory(subscriber));
                    disposeArray.AddDisposable(unsubscriber);

                    if (!item.Method.IsStatic)
                    {
                        if (compositeDis != null)
                        {
                            compositeDis.AddDisposable(unsubscriber);
                        }
                        else if (disCollector != null)
                        {
                            disCollector.Disposes.AddDisposable(unsubscriber);
                        }
                    }
                }

                subscriberTypes[key] = disposeArray;
                return(disposeArray);
            }
        }