Roslyn Update

There have been some changes to Roslyn since my last post and some of my old examples don’t compile.

CustomWorkspace has been replaced by AdHocWorkspace

Creating a class from scratch is now:

AdhocWorkspace cw = new AdhocWorkspace();
OptionSet options = cw.Options;
options = options.WithChangedOption( CSharpFormattingOptions.NewLinesForBracesInMethods, false );
options = options.WithChangedOption( CSharpFormattingOptions.NewLinesForBracesInTypes, false );
SyntaxNode formattedNode = Formatter.Format( cu, cw, options );
StringBuilder sb = new StringBuilder();
using ( StringWriter writer = new StringWriter( sb ) ) {
    formattedNode.WriteTo( writer );
}

Note that some of the CSharpFormattingOptions syntax changed, too.
In our simple getter and setter, the BinaryExpression used for assigment is now an AssignmentExpression.

PropertyDeclarationSyntax property =
    SF.PropertyDeclaration( SF.ParseTypeName( "String" ), SF.Identifier( "A" ) )
        .AddModifiers( SF.Token( SyntaxKind.PublicKeyword ) )
        .AddAccessorListAccessors( 
            SF.AccessorDeclaration(
                SyntaxKind.GetAccessorDeclaration,
                SF.Block(
                    SF.List( new [] {
                        SF.ReturnStatement( SF.IdentifierName( "_a" ) )
                    } )
                )
            ),
            SF.AccessorDeclaration(
                SyntaxKind.SetAccessorDeclaration,
                SF.Block( 
                    SF.List( new [] {
                        SF.ExpressionStatement( 
                            SF.AssignmentExpression( 
                                SyntaxKind.SimpleAssignmentExpression,
                                SF.IdentifierName( "_a" ),
                                SF.IdentifierName( "value" )
                            )
                        )
                    } )
                )
            )
        )
    ;

Everything else from my old example project seems to compile.

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s