Ejemplo n.º 1
0
        public MethodBuildResult CreateMethods(INamedTypeSymbol declaration, CtorBuilderResult ctorResult)
        {
            if (IsSupportedType(declaration))
            {
                var innerMethods    = GetMethods(declaration);
                var externalMethods = declaration.Interfaces.SelectMany(GetMethods);
                var methods         = innerMethods.Concat(externalMethods).ToArray();

                var methodDeclarations = methods
                                         .Select(methodSymbols => CreateMethod(methodSymbols, ctorResult))
                                         .ToArray();

                return(new MethodBuildResult(methodDeclarations));
            }

            throw new ArgumentException("The kind of type must be a interface");
        }
Ejemplo n.º 2
0
        private IEnumerable <StatementSyntax> TryBlock(IMethodSymbol methodSymbol, CtorBuilderResult ctorResult, MethodVariables variables, MethodParameters args)
        {
            var isResult = IsResult(methodSymbol);
            var isAsync  = IsAsync(methodSymbol);

            yield return(Names.ENTER_METHOD.CallMethodSyncStatement(
                             IdentifierName(variables.METHOD_NAME_VARIABLE),
                             IdentifierName(variables.ARGS_VARIABLE)
                             ));

            var arguments = args.Parameters.Select(x => IdentifierName(x.Name)).ToArray();

            if (isResult)
            {
                var resultExpression = isAsync
                    ? ctorResult.OriginFieldIdentifier.InvokeAwaitExpression(methodSymbol.Name, arguments)
                    : ctorResult.OriginFieldIdentifier.InvokeSyncExpression(methodSymbol.Name, arguments);

                yield return(variables.METHOD_RESULT_VARIABLE.VarEqualsStatement(resultExpression));

                yield return(variables.RESULT_VARIABLE.InvokeSyncStatement(Names.METHOD_RESULT_SET_METHOD, IdentifierName(variables.METHOD_RESULT_VARIABLE)));
            }
            else
            {
                yield return(isAsync
                    ? ctorResult.OriginFieldIdentifier.InvokeAwaitStatement(methodSymbol.Name, arguments)
                    : ctorResult.OriginFieldIdentifier.InvokeSyncStatement(methodSymbol.Name, arguments));
            }

            yield return(Names.EXIT_METHOD.CallMethodSyncStatement(
                             IdentifierName(variables.METHOD_NAME_VARIABLE),
                             IdentifierName(variables.ARGS_VARIABLE),
                             IdentifierName(variables.RESULT_VARIABLE)
                             ));

            if (isResult)
            {
                yield return(ReturnStatement(IdentifierName(variables.METHOD_RESULT_VARIABLE)));
            }
        }
Ejemplo n.º 3
0
        private MethodDeclarationSyntax CreateMethod(IMethodSymbol methodSymbol, CtorBuilderResult ctorResult)
        {
            var variables  = new MethodVariables(methodSymbol);
            var parameters = new MethodParameters(methodSymbol);

            var beforTryBody = BeforeTryBlock(methodSymbol, variables, parameters);
            var tryBody      = TryBlock(methodSymbol, ctorResult, variables, parameters).ToArray();
            var catchBody    = CatchBlock(methodSymbol, variables).ToArray();
            var finalyBody   = FinalyBlock(methodSymbol, variables).ToArray();

            var catchClause = CatchClause()
                              .WithDeclaration(CatchDeclaration(_exceptionSymbols.ToQualifiedTypeName())
                                               .WithIdentifier(Identifier(variables.CATCH_BLOCK_EXCEPTION_VARIABLE)))
                              .WithBlock(Block(catchBody));

            var finalyClause = FinallyClause()
                               .WithBlock(Block(finalyBody));

            var tryBlock = TryStatement()
                           .WithBlock(Block(tryBody))
                           .AddCatches(catchClause)
                           .WithFinally(finalyClause);

            var statements = beforTryBody.Append(tryBlock).ToArray();

            var @body = Block(statements);

            var returnType       = methodSymbol.ReturnType.ToQualifiedTypeName();
            var methodName       = Identifier(methodSymbol.Name);
            var methodModifiers  = GetModifiers(methodSymbol);
            var methodParameters = parameters.Parameters.Select(x => x.Syntax).ToArray();

            return(MethodDeclaration(returnType, methodName)
                   .WithModifiers(methodModifiers)
                   .AddParameterListParameters(methodParameters)
                   .WithBody(@body)
                   );
        }