public string GenerateCode(Type interfaceType)
        {
            List <string> methods = new List <string>();
            List <string> fields  = new List <string>();

            int methodIndex = 0;

            foreach (MethodInfo method in GetMethods(interfaceType))
            {
                IProxyMethodWriter writer = GetWriter(method);

                string methodCode  = writer.WriteMethod(methodIndex, method);
                string methodField = writer.WriteField(methodIndex, method);
                methods.Add(methodCode);
                fields.Add(methodField);
                methodIndex++;
            }

            string joinedMethods = JoinLines(methods);
            string joinedFields  = JoinLines(fields);

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

            dictionary["namespace"]            = mNamespaceName;
            dictionary["fields"]               = joinedFields;
            dictionary["implementedMethods"]   = joinedMethods;
            dictionary["implementedInterface"] = FormatTypeExtensions.FormatType(interfaceType);
            dictionary["proxyName"]            = GetInterfaceName(interfaceType);

            string processed = CodeGenerationHelper.ProcessTemplate(mClassDeclarationTemplate, dictionary);

            return(processed);
        }
Beispiel #2
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["interfaceType"] = FormatTypeExtensions.FormatType(type);
            Type returnType = TaskExtensions.UnwrapReturnType(method.ReturnType);

            dictionary["genericType"]           = FormatTypeExtensions.FormatType(returnType);
            dictionary["methodName"]            = method.Name;
            dictionary["qualifiedVariableList"] =
                string.Join(", ",
                            method.GetParameters()
                            .Select(x => GetQualifiedName(x,
                                                          parameter => parameter.Name)));

            dictionary["variableDefaultAssignments"] =
                string.Join(Environment.NewLine,
                            method.GetParameters().
                            Select(parameter => GetDefaultAssignment(parameter)));

            return(CodeGenerationHelper.ProcessTemplate(mFieldDeclaration, dictionary));
        }
Beispiel #3
0
        private string GetDefaultAssignment(ParameterInfo parameter)
        {
            Dictionary <string, string> dictionary = new Dictionary <string, string>();

            dictionary["parameterType"] = FormatTypeExtensions.FormatType(parameter.ParameterType.StripByRef());
            dictionary["parameterName"] = parameter.Name;

            return(CodeGenerationHelper.ProcessTemplate(mDefaultAssignment, dictionary));
        }
Beispiel #4
0
        public string WriteMethod(int methodIndex, MethodInfo method)
        {
            string methodHandler = "mMethodHandler" + methodIndex;

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

            IEnumerable <ParameterInfo> methodCallParameters = method.GetParameters();
            IEnumerable <string>        specialParameters    = Enumerable.Empty <string>();

            if (typeof(Task).IsAssignableFrom(method.ReturnType))
            {
                specialParameters = new[] { FormatTypeExtensions.FormatType(typeof(CancellationToken)) + ".None" };
            }

            if (typeof(Task).IsAssignableFrom(method.ReturnType) &&
                methodCallParameters.LastOrDefault()?.ParameterType == typeof(CancellationToken))
            {
                specialParameters    = new[] { methodCallParameters.LastOrDefault().Name };
                methodCallParameters = methodCallParameters.Take(methodCallParameters.Count() - 1);
            }

            if (method.GetCustomAttribute <WampProgressiveResultProcedureAttribute>() != null)
            {
                specialParameters    = new[] { methodCallParameters.LastOrDefault().Name }.Concat(specialParameters);
                methodCallParameters = methodCallParameters.Take(methodCallParameters.Count() - 1);
            }

            dictionary["methodName"] = method.Name;

            dictionary["parameterList"] =
                string.Join(", ",
                            new[] { "this" }.Concat(specialParameters).Concat(methodCallParameters
                                                                              .Select(x => x.Name)));

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



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

            dictionary["methodHandler"] = methodHandler;

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

            return(CodeGenerationHelper.ProcessTemplate(mMethodTemplate, dictionary));
        }
Beispiel #5
0
        private string GetUnpackStatement(ParameterInfo parameterInfo)
        {
            Dictionary <string, string> dictionary = new Dictionary <string, string>();

            dictionary["parameterType"]  = FormatTypeExtensions.FormatType(parameterInfo.ParameterType.StripByRef());
            dictionary["parameterName"]  = parameterInfo.Name;
            dictionary["parameterIndex"] = parameterInfo.Position.ToString();

            return(CodeGenerationHelper.ProcessTemplate(mUnpackTemplate, dictionary));
        }
Beispiel #6
0
        public string WriteMethod(int methodIndex, MethodInfo method)
        {
            string methodHandler = "mMethodHandler" + 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[] { "this", "___array" });

            Type returnType = method.ReturnType;

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

            if (returnType != typeof(void))
            {
                dictionary["varResult"] = "var ___result = ";
                dictionary["return"]    = "return ___result;";
            }
            else
            {
                dictionary["varResult"] = string.Empty;
                dictionary["return"]    = "return;";
            }

            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)));

            dictionary["methodHandler"] = methodHandler;

            return(CodeGenerationHelper.ProcessTemplate(mMethodTemplate, dictionary));
        }
Beispiel #7
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["interfaceType"] = FormatTypeExtensions.FormatType(type);
            dictionary["methodName"]    = method.Name;
            dictionary["defaults"]      =
                string.Join(", ",
                            method.GetParameters()
                            .Select(x => string.Format("default({0})",
                                                       FormatTypeExtensions.FormatType(x.ParameterType))));

            return(CodeGenerationHelper.ProcessTemplate(mFieldTemplate, dictionary));
        }
        public string WriteMethod(int methodIndex, MethodInfo method)
        {
            string methodHandler = "mMethodHandler" + methodIndex;

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

            ParameterInfo[] parameters = method.GetParameters();

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

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

            if (method.GetCustomAttribute <WampProgressiveResultProcedureAttribute>() != null)
            {
                dictionary["parameterList"] =
                    string.Join(", ",
                                new [] { "this" }.Concat(new[] { parameters.Last() }.Concat(parameters.Take(parameters.Length - 1))
                                                         .Select(x => x.Name)));
            }

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

            dictionary["methodHandler"] = methodHandler;

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

            return(CodeGenerationHelper.ProcessTemplate(mMethodTemplate, dictionary));
        }
Beispiel #9
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));
        }
Beispiel #10
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));
        }
        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));
        }