示例#1
0
        private static ConstructorDeclarationSyntax GenerateConstructor(string name, IEnumerable <DependecyData> contexDependecies)
        {
            var parameters  = contexDependecies.Select(x => SyntaxFactoryEx.Parameter(x.Name.ToLowerFirst(), x.Type));
            var constructor = SyntaxFactory.ConstructorDeclaration(SyntaxFactory.Identifier(name))
                              .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                              .AddParameterListParameters(parameters.ToArray())
                              .WithBody(SyntaxFactory.Block());

            //.AddBodyStatements(SyntaxFactory.Block());
            return(constructor);
        }
        private static List <ExpressionSyntax> GenerateAssigmentExpressions(IEnumerable <Match> matches)
        {
            var assigmentExpressions = new List <ExpressionSyntax>();

            foreach (var match in matches)
            {
                TypeData leftType = match.LeftAccessor.Type;
                var      left     = GenerateExpression(match.LeftAccessor);
                var      right    = GenerateExpression(match.RightAccessor);

                if (leftType.IsCollection && leftType.Arguments.Count() == 1)
                {
                    var arg = leftType.Arguments.First();
                    if (!arg.IsImmutable)
                    {
                        // x => new arg.Name(x)
                        var lambda = SyntaxFactory.SimpleLambdaExpression(SyntaxFactoryEx.Parameter("x"), SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName(arg.Name), SyntaxFactoryEx.ArgumentListWithOneArgument(SyntaxFactory.IdentifierName("x")), null));

                        // rightExpression.Select(lambda)
                        right = SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, right, SyntaxFactory.IdentifierName("Select")), SyntaxFactoryEx.ArgumentListWithOneArgument(lambda));
                    }
                }

                if (!match.LeftAccessor.Type.IsImmutable)
                {
                    if (match.LeftAccessor.Type.IsCollection && match.LeftAccessor.Type.IsInterface)
                    {
                        // rightExpression.ToList()
                        right = SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, right, SyntaxFactory.IdentifierName("ToList")));
                    }
                    else
                    {
                        if (match.LeftAccessor.Type.IsArray)
                        {
                            // rightExpression.ToArray()
                            right = SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, right, SyntaxFactory.IdentifierName("ToArray")));
                        }
                        else
                        {
                            // new LeftReferenceType(rightExpression)
                            right = SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName(match.LeftAccessor.Type.Name), SyntaxFactoryEx.ArgumentListWithOneArgument(right), null);
                        }
                    }
                }


                assigmentExpressions.Add(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, left, right));
            }

            return(assigmentExpressions);
        }
示例#3
0
        private static MethodDeclarationSyntax GenerateHandleMethod(BasicData data)
        {
            var handleMethodReturnType = SyntaxFactory.GenericName("Task").AddTypeArgumentListArguments(data.ReturnType);

            if (data.ReturnType == null)
            {
                handleMethodReturnType = SyntaxFactory.GenericName("Task").AddTypeArgumentListArguments(SyntaxFactory.IdentifierName("Unit"));
            }
            var handleMethod = SyntaxFactory.MethodDeclaration(handleMethodReturnType, "Handle");

            handleMethod = handleMethod.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.AsyncKeyword));
            handleMethod = handleMethod.AddParameterListParameters(SyntaxFactoryEx.Parameter(data.CommandParameterNameInHandleMethod, data.CommandName + data.CommandTypeArguments),
                                                                   SyntaxFactoryEx.Parameter("cancellationToken", "CancellationToken"));
            handleMethod = handleMethod.WithBody(SyntaxFactory.Block());
            return(handleMethod);
        }