Believe it or not, I have written a tool called Roslyn Code Quoter especially to answer this question.
http://roslynquoter.azurewebsites.net
The tool can take any C# program and automatically generate a snippet of code like Matt has written above. Since it also generates everything perfectly including all the whitespace, the code can get rather unwieldy. But you can exclude parts that generate trivia, and then just call NormalizeWhitespace() on the resulting node, it will automatically insert the trivia so that the code is correctly formatted.
For the sake of completeness I'm posting the code in all its gory detail, so that you can see how to construct whitespace and all those little details.
CompilationUnit().WithMembers(
SingletonList<MemberDeclarationSyntax>(
NamespaceDeclaration(
IdentifierName("ACO"))
.WithMembers(
SingletonList<MemberDeclarationSyntax>(
ClassDeclaration("MainForm")
.WithModifiers(
TokenList(
Token(SyntaxKind.PublicKeyword)))
.WithBaseList(
BaseList(
SingletonSeparatedList<BaseTypeSyntax>(
SimpleBaseType(
QualifiedName(
QualifiedName(
QualifiedName(
IdentifierName("System"),
IdentifierName("Windows")),
IdentifierName("Forms")),
IdentifierName("Form"))))))
.WithMembers(
List<MemberDeclarationSyntax>(
new MemberDeclarationSyntax[]{
PropertyDeclaration(
QualifiedName(
QualifiedName(
QualifiedName(
IdentifierName("System"),
IdentifierName("Windows")),
IdentifierName("Forms")),
IdentifierName("Timer")),
Identifier("Ticker"))
.WithModifiers(
TokenList(
Token(SyntaxKind.PublicKeyword)))
.WithAccessorList(
AccessorList(
List<AccessorDeclarationSyntax>(
new AccessorDeclarationSyntax[]{
AccessorDeclaration(
SyntaxKind.GetAccessorDeclaration)
.WithSemicolonToken(
Token(SyntaxKind.SemicolonToken)),
AccessorDeclaration(
SyntaxKind.SetAccessorDeclaration)
.WithSemicolonToken(
Token(SyntaxKind.SemicolonToken))}))),
MethodDeclaration(
PredefinedType(
Token(SyntaxKind.VoidKeyword)),
Identifier("Main"))
.WithAttributeLists(
SingletonList<AttributeListSyntax>(
AttributeList(
SingletonSeparatedList<AttributeSyntax>(
Attribute(
IdentifierName("STAThread"))))))
.WithModifiers(
TokenList(
Token(SyntaxKind.PublicKeyword)))
.WithBody(
Block())}))))))
.NormalizeWhitespace()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…