Esempio n. 1
0
        private IDisposable SetupHandlers(object handler, object fiber, bool regular)
        {
            Type               interfaceType = regular ? typeof(IHandle <>) : typeof(IHandleAsync <>);
            string             subMethod     = regular ? "SubscribeToChannel" : "AsyncSubscribeToChannel";
            IEnumerable <Type> interfaces    = handler.GetType().GetTypeInfo()
                                               .ImplementedInterfaces.Where(x =>
                                                                            x.GetTypeInfo().IsGenericType&& x.GetGenericTypeDefinition() == interfaceType);

            Disposables disposables = new Disposables();

            foreach (Type @interface in interfaces)
            {
                Type       type   = @interface.GetTypeInfo().GenericTypeArguments[0];
                MethodInfo method = @interface.GetRuntimeMethod(regular ? "Handle" : "HandleAsync", new[] { type });

                if (method == null)
                {
                    continue;
                }

                MethodInfo sub = GetType().GetTypeInfo().GetDeclaredMethod(subMethod).MakeGenericMethod(type);

                IDisposable dispose = sub.Invoke(this, new[] { fiber, handler }) as IDisposable;
                disposables.Add(dispose);
            }

            return(disposables);
        }