예제 #1
0
        public static IMethodSymbol OverrideMethod(
            this ISyntaxFactoryService codeFactory,
            IMethodSymbol overriddenMethod,
            SymbolModifiers modifiers,
            INamedTypeSymbol newContainingType,
            Document newDocument,
            CancellationToken cancellationToken)
        {
            // Abstract: Throw not implemented
            if (overriddenMethod.IsAbstract)
            {
                return(CodeGenerationSymbolFactory.CreateMethodSymbol(
                           overriddenMethod,
                           accessibility: overriddenMethod.ComputeResultantAccessibility(newContainingType),
                           modifiers: modifiers,
                           statements: new[] { codeFactory.CreateThrowNotImplementStatement(newDocument.Project.GetCompilationAsync(cancellationToken).WaitAndGetResult(cancellationToken)) }));
            }
            else
            {
                // Otherwise, call the base method with the same parameters
                var typeParams = overriddenMethod.GetTypeArguments();
                var body       = codeFactory.CreateInvocationExpression(
                    codeFactory.CreateMemberAccessExpression(codeFactory.CreateBaseExpression(),
                                                             typeParams.IsDefaultOrEmpty
                        ? codeFactory.CreateIdentifierName(overriddenMethod.Name)
                        : codeFactory.CreateGenericName(overriddenMethod.Name, typeParams)),
                    codeFactory.CreateArguments(overriddenMethod.GetParameters()));

                return(CodeGenerationSymbolFactory.CreateMethodSymbol(
                           method: overriddenMethod,
                           accessibility: overriddenMethod.ComputeResultantAccessibility(newContainingType),
                           modifiers: modifiers,
                           statements: ((IMethodSymbol)overriddenMethod).ReturnsVoid
                        ? new SyntaxNode[] { codeFactory.CreateExpressionStatement(body) }
                        : new SyntaxNode[] { codeFactory.CreateReturnStatement(body) }));
            }
        }