Exemplo n.º 1
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));
        }
Exemplo n.º 2
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["array"] =
                string.Join(", ",
                            (parameters.Select(x => GetCallerParameter(x))));


            dictionary["methodName"] = method.Name;

            dictionary["parameterList"] =
                string.Join(", ",
                            new[] { methodField, "___array" });

            Type returnType = method.ReturnType;

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

            string invokeMethod;

            invokeMethod = "InvokeSync";

            if (returnType != typeof(void))
            {
                dictionary["varResult"]   = "var ___result = ";
                dictionary["return"]      = "return ___result;";
                dictionary["genericType"] = CodeGenerationHelper.GetGenericType(returnType);
            }
            else
            {
                dictionary["varResult"]   = string.Empty;
                dictionary["return"]      = "return;";
                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 =>
                                GetQualifiedName(x, CodeGenerationHelper.GetMethodParameterDeclaration)));

            dictionary["unpack"] =
                string.Join(Environment.NewLine,
                            parameters.Where(x => x.IsOut || x.ParameterType.IsByRef)
                            .Select(x => GetUnpackStatement(x)));

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