简体   繁体   中英

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 GlobalStatementSyntax nodes? And why GlobalStatementSyntax is derived from MemberDeclarationSyntax ? Can such node represent a member of a type?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM