示例#1
0
        private ClassDeclarationSyntax AddRedirectMethods(ClassDeclarationSyntax node)
        {
            var methods = new[]
            {
                MethodDeclaration(IdentifierName("RedirectToRouteResult"), Identifier("RedirectToAction"))
                .WithModifiers(SyntaxKind.ProtectedKeyword)
                .WithAttributes(SyntaxNodeHelpers.CreateGeneratedCodeAttribute(), SyntaxNodeHelpers.CreateDebugNonUserCodeAttribute())
                .AddParameterListParameters(
                    Parameter(Identifier("result")).WithType(IdentifierName("IActionResult")))
                .WithBody(
                    Block(
                        // var callInfo = result.GetR4MvcResult();
                        LocalDeclarationStatement(
                            SyntaxNodeHelpers.VariableDeclaration("callInfo",
                                                                  InvocationExpression(SyntaxNodeHelpers.MemberAccess("result", "GetR4MvcResult")))),
                        // return RedirectToRoute(callInfo.RouteValueDictionary);
                        ReturnStatement(
                            InvocationExpression(IdentifierName("RedirectToRoute"))
                            .WithArgumentList(
                                SyntaxNodeHelpers.MemberAccess("callInfo", "RouteValueDictionary"))))),
                MethodDeclaration(IdentifierName("RedirectToRouteResult"), Identifier("RedirectToAction"))
                .WithModifiers(SyntaxKind.ProtectedKeyword)
                .WithAttributes(SyntaxNodeHelpers.CreateGeneratedCodeAttribute(), SyntaxNodeHelpers.CreateDebugNonUserCodeAttribute())
                .AddParameterListParameters(
                    Parameter(Identifier("taskResult")).WithGenericType("Task", "IActionResult"))
                .WithBody(
                    Block(
                        // return RedirectToAction(taskResult.Result);
                        ReturnStatement(
                            InvocationExpression(IdentifierName("RedirectToAction"))
                            .WithArgumentList(
                                SyntaxNodeHelpers.MemberAccess("taskResult", "Result"))))),
                MethodDeclaration(IdentifierName("RedirectToRouteResult"), Identifier("RedirectToActionPermanent"))
                .WithModifiers(SyntaxKind.ProtectedKeyword)
                .WithAttributes(SyntaxNodeHelpers.CreateGeneratedCodeAttribute(), SyntaxNodeHelpers.CreateDebugNonUserCodeAttribute())
                .AddParameterListParameters(
                    Parameter(Identifier("result")).WithType(IdentifierName("IActionResult")))
                .WithBody(
                    Block(
                        // var callInfo = result.GetR4MvcResult();
                        LocalDeclarationStatement(
                            SyntaxNodeHelpers.VariableDeclaration("callInfo",
                                                                  InvocationExpression(SyntaxNodeHelpers.MemberAccess("result", "GetR4MvcResult")))),
                        // return RedirectToRoutePermanent(callInfo.RouteValueDictionary);
                        ReturnStatement(
                            InvocationExpression(IdentifierName("RedirectToRoutePermanent"))
                            .WithArgumentList(
                                SyntaxNodeHelpers.MemberAccess("callInfo", "RouteValueDictionary"))))),
                MethodDeclaration(IdentifierName("RedirectToRouteResult"), Identifier("RedirectToActionPermanent"))
                .WithModifiers(SyntaxKind.ProtectedKeyword)
                .WithAttributes(SyntaxNodeHelpers.CreateGeneratedCodeAttribute(), SyntaxNodeHelpers.CreateDebugNonUserCodeAttribute())
                .AddParameterListParameters(
                    Parameter(Identifier("taskResult")).WithGenericType("Task", "IActionResult"))
                .WithBody(
                    Block(
                        // return RedirectToActionPermanent(taskResult.Result);
                        ReturnStatement(
                            InvocationExpression(IdentifierName("RedirectToActionPermanent"))
                            .WithArgumentList(
                                SyntaxNodeHelpers.MemberAccess("taskResult", "Result"))))),
            };

            return(node.AddMembers(methods));
        }
示例#2
0
        private ClassDeclarationSyntax AddMethodOverrides(ClassDeclarationSyntax node, ITypeSymbol mvcSymbol)
        {
            const string overrideMethodSuffix = "Override";
            var          methods = mvcSymbol.GetPublicNonGeneratedMethods()
                                   .SelectMany(m =>
            {
                var statements = new List <StatementSyntax>
                {
                    // var callInfo = new R4Mvc_Microsoft_AspNetCore_Mvc_ActionResult(Area, Name, ActionNames.{Action});
                    LocalDeclarationStatement(
                        SyntaxNodeHelpers.VariableDeclaration("callInfo",
                                                              ObjectCreationExpression(IdentifierName(Constants.ActionResultClass))
                                                              .WithArgumentList(
                                                                  IdentifierName("Area"),
                                                                  IdentifierName("Name"),
                                                                  SyntaxNodeHelpers.MemberAccess("ActionNames", m.Name)))),
                };
                foreach (var param in m.Parameters)
                {
                    statements.Add(
                        ExpressionStatement(
                            InvocationExpression(
                                SyntaxNodeHelpers.MemberAccess("ModelUnbinderHelpers", "AddRouteValues"))
                            .WithArgumentList(
                                SyntaxNodeHelpers.MemberAccess("callInfo", "RouteValueDictionary"),
                                LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(param.Name)),
                                IdentifierName(param.Name))));
                }
                statements.Add(
                    // {Action}Override(callInfo, {parameters});
                    ExpressionStatement(
                        InvocationExpression(IdentifierName(m.Name + overrideMethodSuffix))
                        .WithArgumentList(
                            new[] { IdentifierName("callInfo") }
                            .Concat(m.Parameters.Select(p => IdentifierName(p.Name)))
                            .ToArray())));
                statements.Add(
                    // return callInfo;
                    m.ReturnType.ToString().Contains("Task<")
                            ? ReturnStatement(
                        InvocationExpression(
                            SyntaxNodeHelpers.MemberAccess("Task", "FromResult"))
                        .WithArgumentList(
                            BinaryExpression(
                                SyntaxKind.AsExpression,
                                IdentifierName("callInfo"),
                                IdentifierName(m.ReturnType.ToString().Substring(m.ReturnType.ToString().IndexOf('<') + 1).TrimEnd('>')))))
                            : ReturnStatement(IdentifierName("callInfo")));
                return(new[]
                {
                    MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(m.Name + overrideMethodSuffix))
                    .WithModifiers(SyntaxKind.PartialKeyword)
                    .WithAttributes(SyntaxNodeHelpers.CreateNonActionAttribute())
                    .AddParameterListParameters(
                        Parameter(Identifier("callInfo")).WithType(IdentifierName(Constants.ActionResultClass)))
                    .AddParameterListParameters(m.Parameters
                                                .Select(p => Parameter(Identifier(p.Name))
                                                        .WithType(IdentifierName(p.Type.ToString())))
                                                .ToArray())
                    .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)),
                    MethodDeclaration(IdentifierName(m.ReturnType.ToString()), Identifier(m.Name))
                    .WithModifiers(SyntaxKind.PublicKeyword, SyntaxKind.OverrideKeyword)
                    .WithAttributes(SyntaxNodeHelpers.CreateNonActionAttribute())
                    .AddParameterListParameters(m.Parameters
                                                .Select(p => Parameter(Identifier(p.Name))
                                                        .WithType(IdentifierName(p.Type.ToString())))
                                                .ToArray())
                    .WithBody(
                        Block(statements.ToArray())),
                });
            });

            return(node.AddMembers(methods.ToArray()));
        }
示例#3
0
        private ClassDeclarationSyntax AddMethodOverrides(ClassDeclarationSyntax node, ITypeSymbol mvcSymbol)
        {
            const string overrideMethodSuffix = "Override";
            var          methods = mvcSymbol.GetPublicNonGeneratedMethods()
                                   .SelectMany(m =>
            {
                var callInfoType = Constants.ActionResultClass;
                if (m.ReturnType.InheritsFrom <JsonResult>())
                {
                    callInfoType = Constants.JsonResultClass;
                }
                else if (m.ReturnType.InheritsFrom <ContentResult>())
                {
                    callInfoType = Constants.ContentResultClass;
                }
                else if (m.ReturnType.InheritsFrom <RedirectResult>())
                {
                    callInfoType = Constants.RedirectResultClass;
                }
                else if (m.ReturnType.InheritsFrom <RedirectToActionResult>())
                {
                    callInfoType = Constants.RedirectToActionResultClass;
                }
                else if (m.ReturnType.InheritsFrom <RedirectToRouteResult>())
                {
                    callInfoType = Constants.RedirectToRouteResultClass;
                }

                var statements = new List <StatementSyntax>
                {
                    // var callInfo = new R4Mvc_Microsoft_AspNetCore_Mvc_ActionResult(Area, Name, ActionNames.{Action});
                    LocalDeclarationStatement(
                        SyntaxNodeHelpers.VariableDeclaration("callInfo",
                                                              ObjectCreationExpression(IdentifierName(callInfoType))
                                                              .WithArgumentList(
                                                                  IdentifierName("Area"),
                                                                  IdentifierName("Name"),
                                                                  SyntaxNodeHelpers.MemberAccess("ActionNames", m.Name)))),
                };
                foreach (var param in m.Parameters)
                {
                    statements.Add(
                        ExpressionStatement(
                            InvocationExpression(
                                SyntaxNodeHelpers.MemberAccess("ModelUnbinderHelpers", "AddRouteValues"))
                            .WithArgumentList(
                                SyntaxNodeHelpers.MemberAccess("callInfo", "RouteValueDictionary"),
                                LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(param.Name)),
                                IdentifierName(param.Name))));
                }
                statements.Add(
                    // {Action}Override(callInfo, {parameters});
                    ExpressionStatement(
                        InvocationExpression(IdentifierName(m.Name + overrideMethodSuffix))
                        .WithArgumentList(
                            new[] { IdentifierName("callInfo") }
                            .Concat(m.Parameters.Select(p => IdentifierName(p.Name)))
                            .ToArray())));
                var returnType = m.ReturnType as INamedTypeSymbol;
                statements.Add(
                    // return callInfo;
                    returnType.InheritsFrom <Task>() == true
                            ? ReturnStatement(
                        InvocationExpression(
                            SyntaxNodeHelpers.MemberAccess(typeof(Task).FullName, "FromResult"))
                        .WithArgumentList(
                            returnType.TypeArguments.Length > 0
                                            ? (ExpressionSyntax)BinaryExpression(
                                SyntaxKind.AsExpression,
                                IdentifierName("callInfo"),
                                IdentifierName(returnType.TypeArguments[0].ToString()))
                                            : IdentifierName("callInfo")
                            ))
                            : ReturnStatement(IdentifierName("callInfo")));
                return(new[]
                {
                    MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(m.Name + overrideMethodSuffix))
                    .WithModifiers(SyntaxKind.PartialKeyword)
                    .WithNonActionAttribute()
                    .AddParameterListParameters(
                        Parameter(Identifier("callInfo")).WithType(IdentifierName(callInfoType)))
                    .AddParameterListParameters(m.Parameters
                                                .Select(p => Parameter(Identifier(p.Name))
                                                        .WithType(IdentifierName(p.Type.ToString())))
                                                .ToArray())
                    .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)),
                    MethodDeclaration(IdentifierName(m.ReturnType.ToString()), Identifier(m.Name))
                    .WithModifiers(SyntaxKind.PublicKeyword, SyntaxKind.OverrideKeyword)
                    .WithNonActionAttribute()
                    .AddParameterListParameters(m.Parameters
                                                .Select(p => Parameter(Identifier(p.Name))
                                                        .WithType(IdentifierName(p.Type.ToString())))
                                                .ToArray())
                    .WithBody(
                        Block(statements.ToArray())),
                });
            });

            return(node.AddMembers(methods.ToArray()));
        }