Dogs Chasing Squirrels

A software development blog

Code Generation with Roslyn – If, Else, and Loops

1

if statements

This:

MethodDeclarationSyntax method = SF.MethodDeclaration(
    SF.PredefinedType( SF.Token( SyntaxKind.VoidKeyword ) ),
    "MyMethod"
    )
    .AddModifiers( SF.Token( SyntaxKind.PublicKeyword ) )
    .AddBodyStatements( 
        SF.IfStatement(
            SF.BinaryExpression( 
                SyntaxKind.EqualsExpression,
                SF.IdentifierName( "_a" ),
                SF.LiteralExpression( SyntaxKind.NumericLiteralExpression, SF.Literal( "X" ) )
            ),
            SF.Block(  
            )
        )
    )
    ;

Will get you this:

public void MyMethod() {
    if (_a == "X") {
    }
}

if-else statements

This:

MethodDeclarationSyntax method = SF.MethodDeclaration(
    SF.PredefinedType( SF.Token( SyntaxKind.VoidKeyword ) ),
    "MyMethod"
    )
    .AddModifiers( SF.Token( SyntaxKind.PublicKeyword ) )
    .AddBodyStatements( 
        SF.IfStatement(
            SF.BinaryExpression( 
                SyntaxKind.EqualsExpression,
                SF.IdentifierName( "_a" ),
                SF.LiteralExpression( SyntaxKind.NumericLiteralExpression, SF.Literal( "X" ) )
            ),
            SF.Block(  
            ),
            SF.ElseClause( 
                SF.Token( SyntaxKind.ElseKeyword ),
                SF.IfStatement( 
                    SF.BinaryExpression( 
                        SyntaxKind.EqualsExpression,
                        SF.IdentifierName( "_a" ),
                        SF.LiteralExpression( SyntaxKind.NumericLiteralExpression, SF.Literal( "Y" ) )
                    ),
                    SF.Block(  
                    ),
                    SF.ElseClause(
                        SF.Token( SyntaxKind.ElseKeyword ),
                        SF.Block( 
                        )
                    )
                )
            )
        )
    )

Will get you this:

public void MyMethod() {
    if (_a == "X") {
    }
    else if (_a == "Y") {
    }
    else {
    }
}

for loops

This:

MethodDeclarationSyntax method = SF.MethodDeclaration(
    SF.PredefinedType( SF.Token( SyntaxKind.VoidKeyword ) ),
    "MyMethod"
    )
    .AddModifiers( SF.Token( SyntaxKind.PublicKeyword ) )
    .AddBodyStatements( 
        SF.ForStatement(
            SF.VariableDeclaration( 
                SF.PredefinedType( SF.Token( SyntaxKind.IntKeyword ) ), 
                SF.SeparatedList(new [] {
                    SF.VariableDeclarator(
                        SF.Identifier( "i" ),
                        null,
                        SF.EqualsValueClause( SF.LiteralExpression( SyntaxKind.NumericLiteralExpression, SF.Literal( 0 ) ) ) 
                    )
                } )
            ),
            SF.SeparatedList<ExpressionSyntax>(),
            SF.BinaryExpression( 
                SyntaxKind.LessThanExpression,
                SF.IdentifierName( "i" ),
                SF.LiteralExpression( SyntaxKind.NumericLiteralExpression, SF.Literal( 10 ) )
            ),
            SF.SeparatedList<ExpressionSyntax>( new [] {
                SF.PostfixUnaryExpression(
                    SyntaxKind.PostIncrementExpression,
                    SF.IdentifierName( "i" )
                )
            } ),
            SF.Block(  
            )
        )
    )

Will get you this:

public void MyMethod() {
    for (int i = 0; i < 10; i++) {
    }
}

foreach loops

This defines our generic list:

LocalDeclarationStatementSyntax listDeclaration = SF.LocalDeclarationStatement(
    SF.TokenList(),
    SF.VariableDeclaration( 
        SF.GenericName( 
            SF.Identifier( "IList" ), 
            SF.TypeArgumentList( 
                SF.SeparatedList<TypeSyntax>( new [] { SF.PredefinedType( SF.Token( SyntaxKind.StringKeyword ) ) } ) 
            ) 
        ), 
        SF.SeparatedList( new [] {
            SF.VariableDeclarator(
                SF.Identifier( "list" ),
                null,
                SF.EqualsValueClause( 
                    SF.ObjectCreationExpression(
                        SF.Token( SyntaxKind.NewKeyword ),
                        SF.GenericName( 
                            SF.Identifier( "List" ), 
                            SF.TypeArgumentList( 
                                SF.SeparatedList<TypeSyntax>( new [] { SF.PredefinedType( SF.Token( SyntaxKind.StringKeyword ) ) } ) 
                            ) 
                        ),
                        SF.ArgumentList( SF.SeparatedList<ArgumentSyntax>( new ArgumentSyntax[0] ) ),
                        null
                    )
                )
            )
        } )
    )
);

And this our “foreach”:

ForEachStatementSyntax forEachStatement = SF.ForEachStatement(
    SF.PredefinedType( SF.Token( SyntaxKind.StringKeyword ) ),
    SF.Identifier( "item" ),
    SF.IdentifierName( "list" ),
    SF.Block()
);

Adding both to a method:

MethodDeclarationSyntax method = SF.MethodDeclaration(
    SF.PredefinedType( SF.Token( SyntaxKind.VoidKeyword ) ),
    "MyMethod"
    )
    .AddModifiers( SF.Token( SyntaxKind.PublicKeyword ) )
    .AddBodyStatements( 
        listDeclaration,
        forEachStatement
    );

Will get you this:

public void MyMethod() {
    IList<string> list = new List<string>();
    foreach (string item in list) {
    }
}

while loops

This is one of the more straightforward ones.
This:

MethodDeclarationSyntax method = SF.MethodDeclaration(
    SF.PredefinedType( SF.Token( SyntaxKind.VoidKeyword ) ),
    "MyMethod"
    )
    .AddModifiers( SF.Token( SyntaxKind.PublicKeyword ) )
    .AddBodyStatements( 
        SF.WhileStatement( 
            SF.LiteralExpression( SyntaxKind.TrueLiteralExpression ),
            SF.Block(  
                SF.BreakStatement()
            )
        )
    );

Will get you this:

public void MyMethod() {
    while (true) {
        break;
    }
}

One thought on “Code Generation with Roslyn – If, Else, and Loops

Leave a comment