static AsyncMethodCache()
        {
            var methods = typeof(TService)
                          .GetMethods()
                          .Where(mi => !GettersCache <TService> .Handles(mi))
                          .Where(mi => !SettersCache <TService> .Handles(mi))
                          .Where(mi => !IndexedGettersCache <TService> .Handles(mi))
                          .Where(mi => !IndexedSettersCache <TService> .Handles(mi))
                          .Where(mi => mi.IsAsync())
                          .ToArray();

            var delegateMap = methods
                              .Where(mi => !mi.ContainsGenericParameters)
                              .ToDictionary(mi => mi, mi => mi.BuildAsyncDelegate <TService>());

            Methods        = new ConcurrentDictionary <MethodInfo, Func <TService, object[], Task <object> > >(delegateMap);
            GenericMethods = new HashSet <MethodInfo>(methods.Except(Methods.Keys));
        }
Exemplo n.º 2
0
        public static object Invoke(
            TService service,
            TPolicyProvider policyProvider,
            IContextProvider contextProvider,
            MethodInfo methodInfo,
            object[] args)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (policyProvider == null)
            {
                throw new ArgumentNullException(nameof(policyProvider));
            }
            if (contextProvider == null)
            {
                throw new ArgumentNullException(nameof(contextProvider));
            }
            if (methodInfo == null)
            {
                throw new ArgumentNullException(nameof(methodInfo));
            }
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }
            try
            {
                if (GettersCache <TService> .Handles(methodInfo))
                {
                    return(Get(service, policyProvider, methodInfo, contextProvider));
                }

                if (IndexedGettersCache <TService> .Handles(methodInfo))
                {
                    return(Get(service, policyProvider, methodInfo, args, contextProvider));
                }

                if (SettersCache <TService> .Handles(methodInfo))
                {
                    Set(service, policyProvider, methodInfo, args.Single(), contextProvider);
                    return(null);
                }

                if (IndexedSettersCache <TService> .Handles(methodInfo))
                {
                    Set(service, policyProvider, methodInfo, args.Take(args.Length - 1).ToArray(), args.Last(), contextProvider);
                    return(null);
                }

                if (SyncMethodCache <TService> .Handles(methodInfo))
                {
                    return(Call(service, policyProvider, methodInfo, args, contextProvider));
                }

                if (AsyncMethodCache <TService> .Handles(methodInfo))
                {
                    return(CallAsync(service, policyProvider, methodInfo, args, contextProvider));
                }

                throw new NotSupportedException($"The method {methodInfo} is not supported.");
            }
            catch (TargetInvocationException ex)
            {
                throw ex.GetBaseException() ?? ex.InnerException ?? ex;
            }
        }