Dogs Chasing Squirrels

A software development blog

Formatting C# with Roslyn


There’s already been a change to Roslyn that made my last bit of code obsolete. CustomWorkspace no longer requires a string in the constructor and is just:

new CustomWorkspace()

I found out how to change the formatting on the output. We can do this:

CustomWorkspace cw = new CustomWorkspace();
OptionSet options = cw.GetOptions();
options = options.WithChangedOption( CSharpFormattingOptions.OpenBracesInNewLineForMethods, false );
options = options.WithChangedOption( CSharpFormattingOptions.OpenBracesInNewLineForTypes, false );
SyntaxNode formattedNode = Formatter.Format( cu, cw, options );

With this change, our output code is now:

using System;
using System.Generic;

namespace MyNamespace {
    private partial class MyClass {

2 thoughts on “Formatting C# with Roslyn

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: