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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top