Пример #1
0
        private static Type GetRelevantInterceptorType(MethodInfo method)
        {
            Type returnType = method.ReturnType;
            Type genericArgument;
            Type interceptorType;

            if (!typeof(Task).IsAssignableFrom(returnType))
            {
                MethodInfoValidation.ValidateSyncMethod(method);

                genericArgument = returnType == typeof(void) ? typeof(object) : returnType;
                interceptorType = typeof(SyncCalleeProxyInterceptor <>);
            }
            else
            {
                genericArgument = TaskExtensions.UnwrapReturnType(returnType);

                if (method.IsDefined(typeof(WampProgressiveResultProcedureAttribute)))
                {
                    MethodInfoValidation.ValidateProgressiveMethod(method);
                    interceptorType = typeof(ProgressiveAsyncCalleeProxyInterceptor <>);
                }
                else
                {
                    MethodInfoValidation.ValidateAsyncMethod(method);
                    interceptorType = typeof(AsyncCalleeProxyInterceptor <>);
                }
            }

            Type closedGenericType = interceptorType.MakeGenericType(genericArgument);

            return(closedGenericType);
        }
Пример #2
0
        private string GetDelegateType(MethodInfo method)
        {
            string prefix = GetDelegateNamePrefix(method);

            Type returnType = TaskExtensions.UnwrapReturnType(method.ReturnType);

            string returnTypeAlias = FormatTypeExtensions.FormatType(returnType);

            string result = string.Format("{0}Delegate<{1}>", prefix, returnTypeAlias);

            return(result);
        }
Пример #3
0
        public T Invoke <T>(ICalleeProxyInterceptor interceptor, MethodInfo method, IOperationResultExtractor <T> extractor, object[] arguments)
        {
            Type unwrapped = TaskExtensions.UnwrapReturnType(method.ReturnType);

            SyncCallback <T> callback = InnerInvokeSync <T>(interceptor, method, extractor, arguments, unwrapped);

            WaitForResult(callback);

            Exception exception = callback.Exception;

            if (exception != null)
            {
                throw exception;
            }

            return(callback.OperationResult);
        }
Пример #4
0
        public static string GetFormattedReturnType(MethodInfo method)
        {
            if (!method.ReturnsTuple())
            {
                return(FormatType(method.ReturnType));
            }
            else
            {
                Type returnType = TaskExtensions.UnwrapReturnType(method.ReturnType);

                IEnumerable <Type> tupleElementTypes =
                    returnType.GetValueTupleElementTypes();

                TupleElementNamesAttribute tupleElementNamesAttribute =
                    method.ReturnParameter.GetCustomAttribute <TupleElementNamesAttribute>();

                IEnumerable <string> tupleElementsIdentifiers;

                IEnumerable <string> tupleElementTypesIdentifiers = tupleElementTypes
                                                                    .Select(x => FormatType(x));

                if (tupleElementNamesAttribute == null)
                {
                    tupleElementsIdentifiers = tupleElementTypesIdentifiers;
                }
                else
                {
                    tupleElementsIdentifiers =
                        tupleElementTypesIdentifiers
                        .Zip(tupleElementNamesAttribute.TransformNames,
                             (type, name) => $"{type} {name}");
                }

                string tupleIdentifierContent = string.Join(", ", tupleElementsIdentifiers);

                if (typeof(Task).IsAssignableFrom(method.ReturnType))
                {
                    return($"Task<({tupleIdentifierContent})>");
                }
                else
                {
                    return($"({tupleIdentifierContent})");
                }
            }
        }
Пример #5
0
        private static IWampRpcOperation CreateProgressiveOperation(Func <object> instanceProvider, MethodInfo method, string procedureUri)
        {
            //return new ProgressiveAsyncMethodInfoRpcOperation<returnType>
            // (instance, method, procedureUri);

            Type returnType =
                TaskExtensions.UnwrapReturnType(method.ReturnType);

            Type operationType =
                typeof(ProgressiveAsyncMethodInfoRpcOperation <>)
                .MakeGenericType(returnType);

            IWampRpcOperation operation =
                (IWampRpcOperation)Activator.CreateInstance(operationType,
                                                            instanceProvider,
                                                            method,
                                                            procedureUri);

            return(operation);
        }
Пример #6
0
        public string WriteField(int methodIndex, MethodInfo method)
        {
            Dictionary <string, string> dictionary = new Dictionary <string, string>();

            Type type = method.DeclaringType;

            dictionary["methodIndex"]    = methodIndex.ToString();
            dictionary["delegateType"]   = GetDelegateType(method);
            dictionary["delegatePrefix"] = GetDelegateNamePrefix(method);
            dictionary["interfaceType"]  = FormatTypeExtensions.FormatType(type);
            Type genericType = TaskExtensions.UnwrapReturnType(method.ReturnType);

            dictionary["genericType"] = FormatTypeExtensions.FormatType(genericType);
            dictionary["methodName"]  = method.Name;
            dictionary["defaults"]    =
                string.Join(", ",
                            method.GetParameters()
                            .Select(x => $"default({FormatTypeExtensions.FormatType(x.ParameterType)})"));

            return(CodeGenerationHelper.ProcessTemplate(mFieldTemplate, dictionary));
        }
Пример #7
0
        public string WriteMethod(int methodIndex, MethodInfo method)
        {
            string methodField = "mMethod" + methodIndex;

            IDictionary <string, string> dictionary =
                new Dictionary <string, string>();

            ParameterInfo[] parameters = method.GetParameters();

            dictionary["methodName"]    = method.Name;
            dictionary["parameterList"] =
                string.Join(", ",
                            new[] { methodField }.Concat(parameters.Select(x => x.Name)));

            dictionary["returnType"] = FormatTypeExtensions.FormatType(method.ReturnType);

            string invokeMethod;

            if (method.GetCustomAttribute <WampProgressiveResultProcedureAttribute>() != null)
            {
                invokeMethod = "InvokeProgressiveAsync";

                dictionary["parameterList"] =
                    string.Join(", ",
                                new[] { methodField }.Concat
                                    (new[] { parameters.Last() }.Concat(parameters.Take(parameters.Length - 1))
                                    .Select(x => x.Name)));
            }
            else if (typeof(Task).IsAssignableFrom(method.ReturnType))
            {
                invokeMethod = "InvokeAsync";
            }
            else
            {
                invokeMethod = "InvokeSync";
            }

            Type returnType = TaskExtensions.UnwrapReturnType(method.ReturnType);

            if (method.ReturnType != typeof(void))
            {
                dictionary["return"]      = "return ";
                dictionary["genericType"] = CodeGenerationHelper.GetGenericType(returnType);
            }
            else
            {
                dictionary["return"]      = string.Empty;
                dictionary["genericType"] = string.Empty;
            }

            if (method.ReturnType == typeof(Task))
            {
                dictionary["genericType"] = string.Empty;
            }

            if (!method.HasMultivaluedResult())
            {
                invokeMethod = "Single" + invokeMethod;
            }
            else
            {
                invokeMethod = "Multi" + invokeMethod;
                dictionary["genericType"] = CodeGenerationHelper.GetGenericType(returnType.GetElementType());
            }

            dictionary["invokeMethod"] = invokeMethod;

            dictionary["parametersDeclaration"] =
                string.Join(", ",
                            parameters.Select
                                (x => FormatTypeExtensions.FormatType(x.ParameterType) + " " + x.Name));

            return(CodeGenerationHelper.ProcessTemplate(mMethodTemplate, dictionary));
        }