SENya
SENya

Reputation: 1348

What is the "GlobalStatementSyntax" syntax node type in Roslyn

I was investigating classes of syntax nodes which implement MemberDeclarationSyntax in Roslyn and encountered GlobalStatementSyntax class. What kind of code generates syntax tree with GlobalStatementSyntaxnodes? And why GlobalStatementSyntax is derived from MemberDeclarationSyntax? Can such node represent a member of a type?

Upvotes: 0

Views: 291

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239794

Two minutes of experimentation confirmed that you get those if you use the new Top-level statements feature. That is, if your entire program is:

Console.WriteLine(args.Count);

(With no class, no void Main, etc)

Then the Roslyn Quoter generates this structure:

CompilationUnit()
.WithMembers(
    SingletonList<MemberDeclarationSyntax>(
        GlobalStatement(
            ExpressionStatement(
                InvocationExpression(
                    MemberAccessExpression(
                        SyntaxKind.SimpleMemberAccessExpression,
                        IdentifierName("Console"),
                        IdentifierName("WriteLine")))
                .WithArgumentList(
                    ArgumentList(
                        SingletonSeparatedList<ArgumentSyntax>(
                            Argument(
                                MemberAccessExpression(
                                    SyntaxKind.SimpleMemberAccessExpression,
                                    IdentifierName("args"),
                                    IdentifierName("Count"))))))))))
.NormalizeWhitespace()

Upvotes: 3

Related Questions