コード例 #1
0
        private static IEnumerable <MethodDeclarationSyntax> GenerateApiControllerMethods(IEnumerable <MethodDeclarationSyntax> grainInterfaceMethods, string grainName)
        {
            var methodsDeclarations = new List <MethodDeclarationSyntax>();

            foreach (var methodNode in grainInterfaceMethods)
            {
                // insert the id parameter at the end of the list of parameters
                var idParam = RoslynUtils.CreateParameter("string", "id");
                MethodDeclarationSyntax methodDclr = RoslynUtils.AppendParameterToMethod(methodNode, idParam);

                methodDclr = methodDclr.AddModifiers(SF.Token(SyntaxKind.PublicKeyword)).WithSemicolonToken(SF.Token(SyntaxKind.None));

                StatementSyntax getGrainStmt = SF.ParseStatement(string.Format(
                                                                     "var grain = _grainFactory.GetGrain<{0}>(id);\n", grainName));
                MethodInspector methodInspector = new MethodInspector(methodNode);

                string callGrainStmt = string.Format("grain.{0}({1});",
                                                     methodInspector.MethodName, string.Join(", ", methodInspector.MethodParams.Keys));

                if (methodInspector.ReturnType != "Task")
                {
                    callGrainStmt = callGrainStmt.Insert(0, "return ");
                }
                else
                {
                    callGrainStmt = callGrainStmt.Insert(0, "await ");
                    methodDclr    = methodDclr.AddModifiers(SF.Token(SyntaxKind.AsyncKeyword));
                }
                StatementSyntax returnStmt = SF.ParseStatement(callGrainStmt);

                methodsDeclarations.Add(methodDclr.WithBody(SF.Block(getGrainStmt, returnStmt)));
            }
            return(methodsDeclarations);
        }
コード例 #2
0
        private static MethodDeclarationSyntax GenerateMethodDeclaration(MethodInspector methodInspector)
        {
            MethodDeclarationSyntax methodDclr = SF.MethodDeclaration(SF.ParseTypeName(methodInspector.ReturnType), SF.Identifier(methodInspector.MethodName));

            foreach (KeyValuePair <string, string> keyValuePair in methodInspector.MethodParams)
            {
                string paramType = keyValuePair.Value;
                string paramName = keyValuePair.Key;
                methodDclr = RoslynUtils.AppendParameterToMethod(methodDclr, RoslynUtils.CreateParameter(paramType, paramName));
            }
            return(methodDclr);
        }