Frage

I want to create new Method using Roslyn CTP. I create a new instance type of MethodDeclarationSyntax.

 MethodDeclarationSyntax lMethodDeclarationSyntax = Syntax.MethodDeclaration(
            Syntax.List<AttributeListSyntax>(),
            Syntax.TokenList(
                Syntax.Token(SyntaxKind.PublicKeyword)),
            Syntax.IdentifierName("MemoryStream"),
            null,
            Syntax.Identifier("Serialize"),
            null,
            Syntax.ParameterList(),
            Syntax.List<TypeParameterConstraintClauseSyntax>(),
            Syntax.Block(lList));

But in response i get something like this:

publicMemoryStreamSerialize(){MemoryStream lMemoryStream = new MemoryStream();StreamWriter lStreamWriter = new StreamWriter(lMemoryStream);lStreamWriter.Write(IntVariable);lStreamWriter.Write(ExVariable.ToStream());return lMemoryStream;}  

What i missed?

War es hilfreich?

Lösung

Add NormalizeWhitespace at the end to add normalized whitespace trivia into your tree.

MethodDeclarationSyntax lMethodDeclarationSyntax =
    Syntax.MethodDeclaration(
        Syntax.List<AttributeListSyntax>(),
        Syntax.TokenList(Syntax.Token(SyntaxKind.PublicKeyword)),
        Syntax.IdentifierName("MemoryStream"),
        null,
        Syntax.Identifier("Serialize"),
        null,
        Syntax.ParameterList(),
        Syntax.List<TypeParameterConstraintClauseSyntax>(),
        Syntax.Block(lList))
    .NormalizeWhitespace();

You can do this to any syntax node and it will return a node with all the whitespace trivia normalized throughout the tree. Remember that node trees are immutable so it will not have any effect on the already existing tree but will create a new one.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top