Reputation: 1348
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?
Upvotes: 0
Views: 291
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