Subscribe to The Madman Scribblings        RSS Feed
-----

Template Functions via String Interpolation.

Icon Leave Comment
Template Functions via String Interpolation.

I've be toying with the idea of template function in vb.net and c#. (See; Blog and Topic)

Last night I found a way to "sort of" implement in C#6 and VB.net. :shuriken:

I can use SyntaxFactory.ParseExpression and String Interpolation.

    private async Task<Document> AddParameterNullCheckAsync(Document document, ParameterSyntax parameter, BaseMethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
    {

      var nullCheckIfStatement = SyntaxFactory.ExpressionStatement(
                 SyntaxFactory.ParseExpression("if( \{parameter.Identifier} == null ) { throw new ArgumentNullException( nameof( \{parameter.Identifier.Text} )); }"));
      SyntaxList<SyntaxNode> newStatements = methodDeclaration.Body.Statements.Insert(0, nullCheckIfStatement);
      BlockSyntax newBlock = SyntaxFactory.Block(newStatements).WithAdditionalAnnotations(Formatter.Annotation);
      SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken);
      SyntaxNode newRoot = root.ReplaceNode(methodDeclaration.Body, newBlock);

      return document.WithSyntaxRoot(newRoot);
    }



That's a lot better than.
   private async Task<Document> AddParameterNullCheckAsync(Document document, ParameterSyntax parameter, BaseMethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
        {
            BinaryExpressionSyntax binaryExpression = SyntaxFactory.BinaryExpression(
                SyntaxKind.EqualsExpression,
                SyntaxFactory.IdentifierName(parameter.Identifier),
                SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression));

            NameOfExpressionSyntax nameOfExpression = SyntaxFactory.NameOfExpression(
                "nameof",
                SyntaxFactory.ParseTypeName(parameter.Identifier.Text));

            ObjectCreationExpressionSyntax objectCreationExpression = SyntaxFactory.ObjectCreationExpression(
                SyntaxFactory.ParseTypeName(nameof(ArgumentNullException)),
                SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(new[] { SyntaxFactory.Argument(nameOfExpression) })),
                null);

            BlockSyntax syntaxBlock = SyntaxFactory.Block(
                SyntaxFactory.Token(SyntaxKind.OpenBraceToken),
                new SyntaxList<StatementSyntax>().Add(SyntaxFactory.ThrowStatement(objectCreationExpression)),
                SyntaxFactory.Token(SyntaxKind.CloseBraceToken)).WithAdditionalAnnotations(Formatter.Annotation);

            IfStatementSyntax nullCheckIfStatement = SyntaxFactory.IfStatement(
                    SyntaxFactory.Token(SyntaxKind.IfKeyword),
                    SyntaxFactory.Token(SyntaxKind.OpenParenToken),
                    binaryExpression, 
                    SyntaxFactory.Token(SyntaxKind.CloseParenToken), 
                    syntaxBlock, null).WithAdditionalAnnotations(Formatter.Annotation);

            SyntaxList<SyntaxNode> newStatements = methodDeclaration.Body.Statements.Insert(0, nullCheckIfStatement);
            BlockSyntax newBlock = SyntaxFactory.Block(newStatements).WithAdditionalAnnotations(Formatter.Annotation);
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken);
            SyntaxNode newRoot = root.ReplaceNode(methodDeclaration.Body, newBlock);

            return document.WithSyntaxRoot(newRoot);
        }



If the code is a Statement then use ParseExpressionStatement instead.

Once VB.net get String Interpolation it will also be possible do this.

0 Comments On This Entry

 

Search My Blog

Recent Entries

Recent Comments